I am trying to write a script to check someone's details they enter in a HTML form.
I have the code fine but i have realised if they type the postcode in a different format to whats in the database its going to error.
do i just take the POSTed postcode and change the format, if so whats the best way of making whats typed one format?
for example, in the database there is a row with the postal code SS9 5LY
but if someone types it as SS95LY
or ss9 5ly
it wont match
First, take a look to ZIP (POSTAL) Code Validation Regex. Also, try to use a database records in your validation.
Have thousants of approachs to solve your problem.
You can use masked input, you can validate the input text before send to database using RegularExpression. And many others.
You can use preg_replace()
to remove any unwanted characters from the input, convert everything to upper case and finally add the space after the third character using substr()
:
$postcode = preg_replace("/[^a-zA-Z0-9]+/", "", $postcode);
$postcode = strtoupper($postcode);
$postcode = substr($postcode, 0, 3) . ' ' . substr($postcode, 3);