...

View Full Version : Validate Zipcode



petertran123
07-03-2002, 05:13 PM
Hello all,

I'd created a shipping address for my website, and would like to have a code to validate 5 digits number of zipcode. Is there any way i can get it around.

Thanks so much for your help,
Peter :confused:

nolachrymose
07-03-2002, 06:09 PM
var re=/\d{5}/;
if(!re.test(document.forms[0].zipCode.value))
alert("The zipcode entered is not valid!");

Hope that helps!

Happy coding! :)

adios
07-03-2002, 08:25 PM
Courtesy Karen Gayda:

function validateUSZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
States zip code in 5 digit format or zip+4
format. 99999 or 99999-9999

PARAMETERS:
strValue - String to be tested for validity

RETURNS:
True if valid, otherwise false.

*************************************************/
var objRegExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

//check for valid US Zipcode
return objRegExp.test(strValue);
}

whammy
07-04-2002, 02:36 AM
Another way is to validate by state if the country is USA - using what I was able to research from the internet, this is from the treasury department, so I can't guarantee it's totally up to date.



/******************************
Validate US ZIP Code
by Rob Davis
*******************************/
function validate_ZIP(theform){
var ZIP = theform.ZIP.value;
ZIP = Number(ZIP.substring(0,3));
switch(theform.State.options[theform.State.selectedIndex].value){
case "AL": ZIPrange = (ZIP >= 350 && ZIP <= 369);
break;
case "AK": ZIPrange = (ZIP >= 995 && ZIP <= 999);
break;
case "AZ": ZIPrange = (ZIP >= 850 && ZIP <= 865);
break;
case "AR": ZIPrange = ((ZIP >= 716 && ZIP <= 729) || (ZIP == 755));
break;
case "AS": ZIPrange = (ZIP == 967);
break;
case "CA": ZIPrange = (ZIP >= 900 && ZIP <= 966);
break;
case "CO": ZIPrange = (ZIP >= 800 && ZIP <= 816);
break;
case "CT": ZIPrange = (ZIP >= 60 && ZIP <= 69);
break;
case "DC": ZIPrange = (ZIP >= 200 && ZIP <= 205);
break;
case "DE": ZIPrange = (ZIP >= 197 && ZIP <= 199);
break;
case "FL": ZIPrange = ((ZIP >= 320 && ZIP <= 349) && (ZIP != 343 && ZIP !=345 && ZIP!=348));
break;
case "GA": ZIPrange = (ZIP >= 300 && ZIP <= 319);
break;
case "GU": ZIPrange = (ZIP == 969);
break;
case "HI": ZIPrange = (ZIP >= 967 && ZIP <= 968);
break;
case "ID": ZIPrange = (ZIP >= 832 && ZIP <= 838);
break;
case "IL": ZIPrange = (ZIP >= 600 && ZIP <= 629);
break;
case "IN": ZIPrange = (ZIP >= 460 && ZIP <= 479);
break;
case "IA": ZIPrange = (ZIP >= 500 && ZIP <= 528);
break;
case "KS": ZIPrange = (ZIP >= 660 && ZIP <= 679);
break;
case "KY": ZIPrange = (ZIP >= 400 && ZIP <= 427);
break;
case "LA": ZIPrange = (ZIP >= 700 && ZIP <= 714);
break;
case "ME": ZIPrange = (ZIP >= 39 && ZIP <= 49);
break;
case "MH": ZIPrange = (ZIP == 969);
break;
case "MD": ZIPrange = (ZIP >= 206 && ZIP <= 219);
break;
case "MA": ZIPrange = ((ZIP >= 10 && ZIP <= 27) || (ZIP == 55));
break;
case "MI": ZIPrange = (ZIP >= 480 && ZIP <= 499);
break;
case "MN": ZIPrange = (ZIP >= 550 && ZIP <= 567);
break;
case "MS": ZIPrange = (ZIP >= 386 && ZIP <= 397);
break;
case "MO": ZIPrange = (ZIP >= 630 && ZIP <= 658);
break;
case "MT": ZIPrange = (ZIP >= 590 && ZIP <= 599);
break;
case "NE": ZIPrange = (ZIP >= 680 && ZIP <= 693);
break;
case "NV": ZIPrange = (ZIP >= 889 && ZIP <= 898);
break;
case "NH": ZIPrange = (ZIP >= 30 && ZIP <= 38);
break;
case "NJ": ZIPrange = (ZIP >= 70 && ZIP <= 89);
break;
case "NM": ZIPrange = (ZIP >= 870 && ZIP <= 884);
break;
case "NY": ZIPrange = ((ZIP >= 90 && ZIP <= 149) || (ZIP == 4) || (ZIP == 63));
break;
case "NC": ZIPrange = (ZIP >= 269 && ZIP <= 289);
break;
case "ND": ZIPrange = (ZIP >= 580 && ZIP <= 588);
break;
case "MP": ZIPrange = (ZIP == 969);
break;
case "OH": ZIPrange = (ZIP >= 430 && ZIP <= 458);
break;
case "OK": ZIPrange = (ZIP >= 730 && ZIP <= 749);
break;
case "OR": ZIPrange = (ZIP >= 970 && ZIP <= 979);
break;
case "PA": ZIPrange = (ZIP >= 150 && ZIP <= 196);
break;
case "PR": ZIPrange = (ZIP >= 6 && ZIP <= 9);
break;
case "RI": ZIPrange = (ZIP >= 28 && ZIP <= 29);
break;
case "SC": ZIPrange = (ZIP >= 290 && ZIP <= 299);
break;
case "SD": ZIPrange = (ZIP >= 570 && ZIP <= 577);
break;
case "TN": ZIPrange = (ZIP >= 370 && ZIP <= 385);
break;
case "TX": ZIPrange = ((ZIP >= 750 && ZIP <= 799) || (ZIP == 885));
break;
case "UT": ZIPrange = (ZIP >= 840 && ZIP <= 847);
break;
case "VT": ZIPrange = (ZIP >= 50 && ZIP <= 59);
break;
case "VA": ZIPrange = ((ZIP >= 220 && ZIP <= 246) || (ZIP == 201));
break;
case "VI": ZIPrange = (ZIP == 8);
break;
case "WA": ZIPrange = (ZIP >= 980 && ZIP <= 994);
break;
case "WI": ZIPrange = (ZIP >= 530 && ZIP <= 549);
break;
case "WV": ZIPrange = (ZIP >= 247 && ZIP <= 268);
break;
case "WY": ZIPrange = (ZIP >= 820 && ZIP <= 831);
break;
case "AE": ZIPrange = (ZIP >= 90 && ZIP <= 98);
break;
case "AA": ZIPrange = (ZIP == 340);
break;
case "AP": ZIPrange = (ZIP >= 962 && ZIP <= 966);
break;
default: ZIPrange = (/^\d{3}$/.test(ZIP));
}
return ZIPrange;
}
/******************************
End US ZIP Code Validation
*******************************/

whammy
07-04-2002, 02:46 AM
Also, I have some regular expresssions (thanks to the supermod for turning me on to these ;)) with US or CANADA, it might help...



var vzip1 = /^\D*(\d{5})$/
var vzip2 = /^\D*(\d{5})\D*(\d{4})\D*$/
var vpostcode = /^\W*([a-z]{1}\d{1}[a-z]{1})\W*(\d{1}[a-z]{1}\d{1})\W*$/i
/* Format US/Canadian Postal Code */
function formatzippostal(zip){
if(vpostcode.test(zip.value)){
zip.value = zip.value.toUpperCase();
zip.value=zip.value.replace(vpostcode,'$1 $2');
}
else if(!vzip1.test(zip.value)){
if(vzip2.test(zip.value)){
zip.value=zip.value.replace(vzip2,'$1-$2');
}
}
}

petertran123
07-08-2002, 02:06 PM
:) Thanks for all your help is it very nice of you all. Thanks again.

RadarBob
07-08-2002, 04:09 PM
so I can't guarantee it's totally up to date

Never guarantee someone else's code. :cool:

That code checking by state is great, very specific & therefore better validation - with potentially better user feedback. You should put in specific user messages such as "xxxxx is not a valid Texas zip code." Otherwise, you totally waste your time and the computer's time, and what's the point of doing all that checking?

Personally, my immediate concern is how I actually maintain the code. I'd call the local postmaster and ask how I can keep current with zip code changes.

whammy
07-09-2002, 05:43 AM
Actually the "rest" of the code has that in there...

dragon
07-25-2007, 09:51 PM
I hope this isn't disrupting anything by posting on an old thread, but I haven't seen any others related.

A co-worker and I are working on a project where we may need to validate, or at least accept, foreign postal codes. Does anyone have any general information about postal codes outside of the US? I've seen some sites that show 4 digit postal codes for some countries, but many sites have listed no information for postal codes.

Philip M
07-25-2007, 10:54 PM
I think it might have been better to start a new thread. But...


UK Postcode (simple - checks for valid format but not valid code):-

if (/^[A-Z]{1,2}\d{1,2}[A-Z]?\s\d[A-Z]{2}$/.test(postcode.value)) {
(N.B. space required between first (outcode) and second (incode) part of postcode.)

UK Postcode (detailed - checks for valid format and valid code):-

if (/((^(A[BL]|B[ABDHLNRST]|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]|F[KY]|G[LUY]|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]|M[EKL]|N[EGNPRW]|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKL-PRSTWY]|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)\d\d?)|(^W1[A-HJKSTUW0-9])|(^WC[1-2])|(^EC[1-4])|(^SW1[ABEHMNPRVWXY])|(^GIR\s?0AA))(\s\d[ABDEFGHJLNPQRSTUWXYZ]{2})$/.test(postcode.value)) {

For example, AB and AL are valid postal town codes, but not AC, AD etc.
(N.B. Space required between first (outcode) and second (incode) part of postcode.)
Letters C, I, K, M, O and V are never used in the incode.

Canadian postcode format:-

if (/^([A-Z]\d[A-Z][-\s]?\d[A-Z]\d)$/.test(postcode.value)) {
(letter - digit - letter - optional space - digit -letter - digit)


Netherlands postcode format:-

if (/^[1-9]{1}[0-9]{3}\s?[A-Z]{2}$/.test(postcode value)) {
(4 digits (the first one not 0) - optional space - 2 letters)

France and Germany:-

if (/^[1-9]{1}[0-9]{4}$/.test(postcode.value)) {
(5 digits, the first one not 0)


All above codes must be entered in UPPERCASE letters. If lowercase permitted add /i switch.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum