Jump to content

Postcode validation


wiggst3r

Recommended Posts

I'm trying to implement postcode validation (UK) in a form I've wrote.

All works fine, If I include a space, e.g. AB12 3CD, but If I have the whole thing written as AB123CD, I can't get it to work and I get a validation error.

 

My code to display the error is as follows:

 

		
if(!str_replace(' ', '', $v->valid_postcode($data['post_code'])))
{
$errors[] = "You must enter a valid postcode";
}

 

Postcode validation function:

 

function valid_postcode($postcode) {
  $postcode = strtoupper($postcode);
  if(ereg("((GIR 0AA)|(TDCU 1ZZ)|(ASCN 1ZZ)|(BIQQ 1ZZ)|(BBND 1ZZ)"
."|(FIQQ 1ZZ)|(PCRN 1ZZ)|(STHL 1ZZ)|(SIQQ 1ZZ)|(TKCA 1ZZ)"
."|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]"
."|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))"
."|[0-9][A-HJKS-UW]) [0-9][ABD-HJLNP-UW-Z]{2})", $postcode)) {
      return $postcode;
} else {
     return FALSE;
}

 

I want it, so no matter what I type, AB12 3CD or AB123CD, it lets me use this.

 

Any ideas where I'm going wrong?

 

Thanks

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.