Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-03-2002, 04:13 PM   PM User | #1
petertran123
Regular Coder

 
Join Date: Jul 2002
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
petertran123 will become famous soon enough
Validate Zipcode

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
petertran123 is offline   Reply With Quote
Old 07-03-2002, 05:09 PM   PM User | #2
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
Code:
var re=/\d{5}/;
if(!re.test(document.forms[0].zipCode.value)) 
alert("The zipcode entered is not valid!");
Hope that helps!

Happy coding!
nolachrymose is offline   Reply With Quote
Old 07-03-2002, 07:25 PM   PM User | #3
adios
Senior Coder

 
Join Date: Jun 2002
Posts: 1,404
Thanks: 2
Thanked 32 Times in 32 Posts
adios is on a distinguished road
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);
}
adios is offline   Reply With Quote
Old 07-04-2002, 01:36 AM   PM User | #4
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
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.

Code:
/******************************
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
*******************************/
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)
whammy is offline   Reply With Quote
Old 07-04-2002, 01:46 AM   PM User | #5
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
Also, I have some regular expresssions (thanks to the supermod for turning me on to these ) with US or CANADA, it might help...

Code:
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');
		}
	}
}
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)

Last edited by whammy; 07-04-2002 at 01:50 AM..
whammy is offline   Reply With Quote
Old 07-08-2002, 01:06 PM   PM User | #6
petertran123
Regular Coder

 
Join Date: Jul 2002
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
petertran123 will become famous soon enough
Thanks for all your help is it very nice of you all. Thanks again.
petertran123 is offline   Reply With Quote
Old 07-08-2002, 03:09 PM   PM User | #7
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Quote:
so I can't guarantee it's totally up to date
Never guarantee someone else's code.

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.
RadarBob is offline   Reply With Quote
Old 07-09-2002, 04:43 AM   PM User | #8
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
Actually the "rest" of the code has that in there...
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)
whammy is offline   Reply With Quote
Old 07-25-2007, 08:51 PM   PM User | #9
dragon
New Coder

 
dragon's Avatar
 
Join Date: May 2003
Location: Florida
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
dragon is an unknown quantity at this point
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.
__________________
http://www.dragonshobbies.com
dragon is offline   Reply With Quote
Old 07-25-2007, 09:54 PM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.

Last edited by Philip M; 07-25-2007 at 10:35 PM.. Reason: Added a format
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:51 PM.


Advertisement
Log in to turn off these ads.