charlie
charlie

Reputation: 1384

PHP check postal code in MySQL Database

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

Upvotes: 0

Views: 1116

Answers (3)

Petr R.
Petr R.

Reputation: 1237

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);

Upvotes: 0

Guerra
Guerra

Reputation: 2790

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.

Upvotes: 0

Mihai8
Mihai8

Reputation: 3147

First, take a look to ZIP (POSTAL) Code Validation Regex. Also, try to use a database records in your validation.

Upvotes: 1

Related Questions