Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate US Zip Code format with Rails

How do you validate a U.S. zip code using Rails?

I wrote something like this but it doesn't work:

validates_format_of :zip_code,
  :with => /^\d{5}(-\d{4})?$/,
  :message => "Zip code should be valid"

like image 300
Michał Makaruk Avatar asked Nov 21 '11 13:11

Michał Makaruk


People also ask

How do I verify a US ZIP Code?

US ZIP code (U.S. postal code) allow both the five-digit and nine-digit (called ZIP + 4) formats. E.g. a valid postal code should match 12345 and 12345-6789, but not 1234, 123456, 123456789, or 1234-56789. ^ # Assert position at the beginning of the string. [0-9]{5} # Match a digit, exactly five times.

What is the regular expression used to validate a US ZIP Code?

You need to validate a ZIP code (U.S. postal code), allowing both the five-digit and nine-digit (called ZIP+4) formats. The regex should match 12345 and 12345-6789 , but not 1234 , 123456 , 123456789 , or 1234-56789 .

What is the format for ZIP Code in USA?

A United States postal code, for example, might be either five digits or five digits followed a hyphen (dash) and another four digits (12345-1234). If you are validating US postal codes, allow for both conditions. The following rule validates a field that can be five digits, five digits + dash + 4 digits, or blank.

Is 11111 a valid US ZIP Code?

11111 is not a valid 5 digit ZIP Code, but there are valid ZIP Codes that start with 111: 11101, 11102, etc. Once position 28 was changed to Y, then MSP considered 11111 as an Invalid zip, because it looked at all 5 digits.


1 Answers

You can also validate that it is actually a valid zip (not just the format but the zip itself) using:
http://www.webservicex.net/uszip.asmx?op=GetInfoByZIP

Try a valid zip you know, e.g. 02135 vs an invalid one like 09990 to see the difference.

I would consider combining this with:

validates_format_of :zip, :with => /^\d{5}(-\d{4})?$/, :message => "should be in the form 12345 or 12345-1234"

that it's done with validate_format_of, rather than validate_format_of_zip_code as that means it can also be used for phone numbers, etc that also fit a fixed, known, format (.e.g. U.S.).
Perhaps validate format first and give error if invalid, so handle it all within rails with standard flash message.
Then, if valid make the call to that server to validate actual zip itself.

The only downside to great server supplied validations like this is that they increase the dependency on other sites and services. So if the other site/service changes things or is unavailable, etc. there is an issue. This is another reason why doing the simpler validity check first is a great idea.

A full service solution would also check for time-out by the zip code service and if that happens, say 5 seconds and the format is ok probably best to accept value. Maybe set an 'unverified_zip' flag if possible!

like image 126
Michael Durrant Avatar answered Sep 19 '22 23:09

Michael Durrant