Postcode Regex

I have a form where one of the fields asks the user to enter a postcode.
Where i am handling the form i have a simple regex to try and stop invalid postcodes passing through, however when i am testing the program its not even allowing me to enter real postcodes.
What have i done wrong?
Below is my code…

// Check for a postcode:
if (preg_match (’/^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/g/’, $trimmed[‘postcode’])) {
$pcde = mysqli_real_escape_string ($dbc, $trimmed[‘postcode’]);
$pcde = StrToUpper($pcde);
} else {
echo ‘

Postcode - Pleae enter a valid postcode!

’;
}

Thanks.

One, drop the ‘g/’

The global modified is not recognized in any PHP script I have attempted to use it in and the last forward slash shouldn’t be there.

Since this looks to be for across the pond, what would be a valid Postal Code format? The regex is kind of mixed and is likely causing issues. You are also only allowing uppercase characters, then converting to uppercase if it validates.

Also, it’s strtoupper not StrToUpper, unless you wrote your own.

Sponsor our Newsletter | Privacy Policy | Terms of Service