asfasdfasdf
asfasdfasdf

Reputation:

where can i get regex for US / Canadian / Western Europe Postal Code, address, phone, fax etc?

so im looking for a regex or some solution to detect street address, phone, fax etc in western countries.

i know it wont be perfect, but still my priority is on US and Canadian street addresses, province/state, postal code and etc....

it would be nice if someone went out and did this already, instead of me rewriting the regex...

Upvotes: 0

Views: 1944

Answers (4)

Nathan Koop
Nathan Koop

Reputation: 25197

Using information I got from this question I searched http://regexlib.com and found what you are looking for

This matches either postal code or zip

^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$

Phone or fax:

^\+[0-9]{1,3}\([0-9]{3}\)[0-9]{7}$

Like Ben has mentioned, you won't be able to verify whether or not the address is valid or not, but you can verify that the format is correct.

Upvotes: 0

Balram Singh
Balram Singh

Reputation: 1744

You may try this regex, this will work for all Us and Canadian zip postal codes. eg A1A 1A1 Canadian Zip Codes and eg 99999 or 99999 8989 Us Zip Codes

(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)

Upvotes: 0

Ben S
Ben S

Reputation: 69342

Canadian postal codes can be verified though Canada Post's website.

It returns a range of valid addresses given a postal code. I am not sure if there's a web API for it, but it could provide much better accuracy than a regex.

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 400972

You can probably find some interesting stuff in the sub-packages of PEAR::Validate (it's in PHP) that correspond to the locales you want

For instance, in the Validate_US class :

function postalCode($postalCode, $strong = false)
{
    return (bool)preg_match('/^[0-9]{5}((-| )[0-9]{4})?$/', $postalCode);
}

The same method, in the Validate_FR class :

function postalCode($postalCode, $strong = false)
{
      return (bool) preg_match('/^(0[1-9]|[1-9][0-9])[0-9][0-9][0-9]$/', 
                               $postalCode);
}


But note that this kind of regex will only allow you to validate that an given code looks valid, not that it actually is valid : there are so many postal codes (and even more addresses !), the list would be un-manageable, and a maintenance nightmare, I guess.

Upvotes: 0

Related Questions