PDA

View Full Version : Having Trouble With a Zip Code State Verification Script


John_Saunders
08-28-2002, 09:32 PM
I found the script below on Google that makes sure the visitor enters a zip code that matches the state they enter. I can't get it to work for some reason and I was wondering if anybody can point out any errors. It looks like the myzip in the third function should be zip correct?


<script language="JavaScript" type="text/javascript">
<!--
// Expects a 5-digit zip code. Prevents user from entering 5 identical digits, as in 44444.

function checkZipDigitsIdentical(zip){
sample=zip.substring(0,1)
for(i=1;i<zip.length;i++){
if ((zip.substring(i,i+1))!=sample){
return
}
}
error=1
msg=zip+" is not an acceptable zip code."
}

// Expects digits only, and checks that they aren't sequential, i.e. 23456 or 65432

function checkZipDigitsSequential(zip){
first=zip.substring(0,1)
second=zip.substring(1,2)
diff=parseInt(first)-parseInt(second)
if(Math.abs(diff)!=1) return
if (diff == -1){ //ascending order
for(i=0; i<zip.length-1; i++){
if ((parseInt(zip.substring(i,i+1)))!=(parseInt(zip.substring(i+1,i+2))-1)) return
}
}
if (diff == 1){ //descending order
for(i=0;i<zip.length-1;i++){
if ((parseInt(zip.substring(i,i+1)))!=(parseInt(zip.substring(i+1,i+2))+1)) return
}
}
error=1
msg=zip+" is not an acceptable zip code."
}

// Check to make sure the zip code matches the state.

function checkIllegalZip(myzip,state) {
var ZipAA = "340";
var ZipAE = "090,091,092,093,094,095,096,097,098";
var ZipAK = "995,996,997,998,999";
var ZipAL = "350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,393";
var ZipAP = "962,963,964,965,966,968";
var ZipAR = "381,716,717,718,719,720,721,722,723,724,725,726,727,728,729,755";
var ZipAS = "967";
var ZipAZ = "850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,873,879,880";
var ZipCA = "895,900,901,902,903,904,905,906,907,908,910,911,912,913,914,915,916,917,918,918,920,921,922,923,924, 925,926,927,928,929,930,931,932,933,934,935,936,937,938,940,941,942,943,944,945,946,947,948,949,950, 951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966";
var ZipCO = "800,801,802,803,805,806,807,808,809,810,811,812,813,814,815,816";
var ZipCT = "060,061,062,063,064,065,066,067,068,069";
var ZipDC = "200,201,202,203,204,205";
var ZipDE = "197,198,199,216,218";
var ZipFL = "315,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343, 344,345,346,347,348,349";
var ZipGA = "298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,374,399";
var ZipHI = "967,968,969";
var ZipIA = "500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524, 525,526,527,528,612,681";
var ZipID = "832,833,834,835,836,837,838,970,990";
var ZipIL = "527,528,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622, 623,624,625,626,627,628,629,631,634,635";
var ZipIN = "400,420,423,424,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479";
var ZipKS = "641,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,678,679,739";
var ZipKY = "400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424, 425,426,427,452,471,476";
var ZipLA = "700,701,702,703,704,705,706,707,708,709,710,711,712,713,714,717";
var ZipMA = "010,011,012,013,014,015,016,017,018,019,020,021,022,023,024,025,026,027,055";
var ZipMD = "203,206,207,208,209,210,211,212,213,214,215,216,217,218,219,267";
var ZipME = "038,039,040,041,042,043,044,045,046,047,048,049";
var ZipMI = "480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499";
var ZipMN = "550,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,540,581";
var ZipMO = "621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,637,638,639,640,641,642,643,644,645,646, 647,648,649,650,651,652,653,654,655,656,657,658";
var ZipMS = "369,381,386,387,388,389,390,391,392,393,394,395,396,397";
var ZipMT = "590,591,592,593,594,595,596,597,598,599,821";
var ZipNC = "270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,297";
var ZipND = "565,557,580,581,582,583,584,585,586,587,588";
var ZipNE = "515,516,680,681,682,683,684,685,686,687,688,689,690,691,692,693";
var ZipNH = "030,031,032,033,034,035,036,036,037,038,039,050";
var ZipNJ = "070,071,072,073,074,075,076,077,078,079,080,081,082,083,084,085,086,087,088,089";
var ZipNM = "865,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884";
var ZipNV = "889,890,891,892,893,894,895,896,897,898,961";
var ZipNY = "003,004,005,090,091,092,093,094,095,096,097,098,099,100,101,102,103,104,105,106,107,108,109,110,111, 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136, 137,138,139,140,141,142,143,144,145,146,147,148,149";
var ZipOH = "410,430,431,432,433,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454, 455,456,457,458,459,470";
var ZipOK = "730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,748,749,679";
var ZipOR = "836,970,971,972,973,974,975,976,977,978,979,986";
var ZipPA = "150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174, 175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,213,260";
var ZipPR = "006,007,008,009";
var ZipRI = "027,028,029";
var ZipSC = "282,290,291,292,293,294,295,296,297,298,299,309,314";
var ZipSD = "570,571,572,573,574,575,576,577";
var ZipTN = "307,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,723";
var ZipTX = "718,733,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772, 773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797, 798,799,885";
var ZipUT = "840,841,842,843,844,845,846,847,893,898";
var ZipVA = "201,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243, 244,245,246";
var ZipVI = "008";
var ZipVT = "050,051,052,053,054,055,056,057,058,059,035,036,037";
var ZipWA = "835,838,972,980,981,982,983,984,985,986,987,988,989,990,991,992,993,994";
var ZipWI = "498,499,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,548,549,551";
var ZipWV = "246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268";
var ZipWY = "591,820,821,823,824,825,826,827,828,829,830,831";
var zip3 = myzip.value.substring(0,3)
if( eval("Zip" + state.value.toUpperCase()).indexOf(zip3,0) == -1 ) {
error = 1
msg = "'" + myzip.value + "' is not a valid zip code for state " + state.value.toUpperCase() + "."
}
}

//-->
</script>

<form action="" name="test" onSubmit="checkZipDigitsSequential(zip)">
<input type="text" name="state">
<br>
<input type="text" name="zip">
<br>
<input type="submit" value="Go">
</form>

Or if you can't figure it out from the code above, please see if anybody can get number 24 to work at the following website: http://www.chrismcgrath.com/pagebuilder.php3?crossreference=53&partid=162

It just comes up and reloads the page without an error message when I try to do it.

Any help would be greatly appreciated.


Thanks,

John

beetle
08-28-2002, 09:50 PM
I've been looking for that ZIP/state data for weeks to incorporate it into my validation routine (http://www.peterbailey.net/fValidate)

I'll see if I can get it working. That script below has alot of redundancies that can be handled better with regular expressions.

myzip does look out of place.

John_Saunders
08-28-2002, 09:58 PM
I was just doing some searching on Google after I made the post above to see if I could find another script like this and I came across your fValidate script and bookmarked it! :) It looks awesome.

The only other one I could find was the US Treasury's Zip Code validator in this forum but it didn't display any error message. Besides, the one I posted above seems better.

Do you know when you could look at the getting the script above working? Did you mean today or something within the next week? I've been working on trying to get this form I'm using to work all day but this thing is hanging me up.


John

beetle
08-28-2002, 10:16 PM
Cool. I didn't realize I ranked so high on Google. :)

If I'm lucky, I'll get to it this week. Lots of new stuff rolling out for fValidate soon ;)

John_Saunders
08-28-2002, 10:20 PM
Cool! Do you have any ETA for the API?


John

beetle
08-28-2002, 10:36 PM
Uhhh. Uhhh. Tonight if I can find the time. If not, before the weekend.

whammy
08-29-2002, 12:21 AM
Try my old one:


/******************************
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
*******************************/


I know this one works but you might have to tweak it a bit... for example the script you're using allows 363XX for Alaska but mine doesn't (and I don't know where that script's information came from, so it might be newer (or older) than mine), so you might have to add stuff like that in - but that's probably easier than the way the other script works.

P.S. If you have an up-to-date Zip validation reference, I'd appreciate the URL myself! I don't usually use State/Zip validation (I assume if people want something delivered to them in a timely fashion, that they'll provide the right zip code!) but it might be handy down the road.

I actually made this script up from a treasury department .pdf document that I found on the 'net, but there was no reference date anywhere...

John_Saunders
08-29-2002, 12:39 AM
Thanks for your reply Whammy. I dug up one of your old posts with this script, but unfortunately I"m not very familiar with Javascript so I'm not sure how to do this. Could you tell me how to add something to your script so an error message will pop up if the visitor doesn't enter a zip code that matches the state? Something like this:

msg = "'" + myzip.value + "' is not a valid zip code for state " + state.value.toUpperCase() + "."
}

Here is the link to where I found the script I posted: http://www.chrismcgrath.com/pagebuilder.php3?crossreference=53&partid=162 I'm not sure where he got it but it says his page was last updated in February, so I guess if he did all that himself it is current as of 6 months ago.

I know what you mean about the zip code but the script would be useful from keeping people from entering bogus info. For example, in this form I'm using people need to enter their info to access a certain page. The problem is people just pick the first state with a bunch of numbers. By using this script it will make them pick a zip code that matches a state which would probably make them just enter one they know matches, (THEIR OWN).


Regards,

John

John_Saunders
08-30-2002, 02:19 PM
whammy,

I tried using your script but if I enter an invalid state and Zip in the State and ZIP form fields, nothing happens and I named the form name="theform". Can you or anybody else tell me how to print a popup window on the screen informing the submitter the zip code and state do not match?

If somebody can help me get to this point I will go through and make sure all the zip codes are updated and re-post it for everyone to use.

Any help would be greatly appreciated.


Thanks,

John

whammy
08-30-2002, 04:14 PM
I'll help you with it tonight as soon as I get home.

whammy
08-31-2002, 12:01 AM
Ok, sorry about that - I was at work and working on some interesting stuff (and busy) - can I see the current form (a URL is preferable to posting the code here) that you're using? Then it should be easy to modify the above script to work with your form.

:)

John_Saunders
08-31-2002, 01:17 AM
I don't have a site that I am posting this to because I'm working off my local server but if you copy the code below and save it into an .html page, you can test it right off your hard drive. Here's what I'm using now:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<script language="JavaScript" type="text/JavaScript">
<!--
/* Validate US ZIP Code by Rob Davis */
function validate_ZIP(contact){
var ZIP = theform.ZIP.value;
ZIP = Number(ZIP.substring(0,3));
switch(contact.State.options[contact.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;
}
//-->
</script>
</head>

<body>
<form action="test.asp" name="contact">
<table border="0" cellpadding="4" cellspacing="4" width="400">
<tr>
<td width="124">First name</td>
<td width="346"><input name="FirstName" type="text" size="24"></td>
</tr>
<tr>
<td>Last name</td>
<td><input type="text" name="LastName" size="24"></td>
</tr>
<tr>
<td>State</td>
<td>
<select name="State">
<option value="" selected>Please select</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="CT">CT</option>
<option value="CO">CO</option>
<option value="DC">DC</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="IA">IA</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="MA">MA</option>
<option value="MD">MD</option>
<option value="ME">ME</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MO">MO</option>
<option value="MS">MS</option>
<option value="MT">MT</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="NE">NE</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NV">NV</option>
<option value="NY">NY</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WI">WI</option>
<option value="WV">WV</option>
<option value="WY">WY </option>
</select>
</td>
</tr>
<tr>
<td>Zip Code</td>
<td><input type="text" name="Zip" size="5"> </td>
</tr>
<tr>
<td>Number</td>
<td><input type="text" name="Phone" size="24"> </td>
</tr>
<tr>
<td>E-mail</td>
<td><input type="text" name="Email" size="24"></td>
</tr>
</table>
<br>
<input name="submit" type="submit" value="Continue">
</form>
</body>
</html>


John

whammy
08-31-2002, 01:20 AM
Ugh... no offense... but there is NO way I'm going to wade through macromedia code. I would never use DreamWeaver to validate anything just because of the horribly cryptic code it creates.

How about you just post your HTML (no javascript at all) for the form and let us know what you want to validate for?

I'd much rather write a validation script than wade through that mess! No wonder it isn't working.


Even better yet: Just make the HTML form with no validation whatsover (from scratch - I would throw that code away the second I saw it!).

Then tell us what you want to validate for... I'm sure we can make it much simpler and much more understandable (and probably more accurate) than anything that DreamWeaver can concoct.


:D

John_Saunders
08-31-2002, 01:23 AM
I understand completely! I edited my previous posted and pasted the code there minus the DW javascript.

I'd like for all the fields to be required. If you can easily make the phone number field require ten digits that would be great too. :)

Thanks for your help.


John

whammy
08-31-2002, 01:58 AM
Wow... that shouldn't have been that hard! LOL - but it took me way too long to figure out all the discrepancies. :(

This works... I'd suggest scrapping everything to this point and starting from here... especially since I adjusted a lot of stuff to your form. But this works.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<script language="JavaScript" type="text/JavaScript">
<!--
/* Validate US ZIP Code by Rob Davis */
function ValidZip(zip){
var ZIP = Number(zip.substring(0,3));
alert(ZIP);
alert(document.contact.State.options[contact.State.selectedIndex].value);
switch(contact.State.options[document.contact.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: return false;
}
return ZIPrange;
}
//-->
</script>
</head>

<body>
<form name="contact" action="test.asp" onsubmit="return ValidZip(this.Zip.value)">
<table border="0" cellpadding="4" cellspacing="4" width="400">
<tr>
<td>State</td>
<td>
<select name="State">
<option value="" selected>Please select</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="CT">CT</option>
<option value="CO">CO</option>
<option value="DC">DC</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="IA">IA</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="MA">MA</option>
<option value="MD">MD</option>
<option value="ME">ME</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MO">MO</option>
<option value="MS">MS</option>
<option value="MT">MT</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="NE">NE</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NV">NV</option>
<option value="NY">NY</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WI">WI</option>
<option value="WV">WV</option>
<option value="WY">WY </option>
</select>
</td>
</tr>
<tr>
<td>Zip Code</td>
<td><input type="text" name="Zip" size="5"></td>
</tr>
</table>
<br />
<input name="submit" type="submit" value="Continue" />
</form>
</body>
</html>

whammy
08-31-2002, 02:02 AM
As for the phone number requiring 10 digits - check out this script (available on my site, thanks to JKD for his original help with regular expressions!):

<script type="text/javascript">
<!--
var vphone = /^\D*(1)?\D*([1-9]\d{2})\D*(\d{3})\D*(\d{4})\D*$/
function formatphone(x){
if(vphone.test(x.value)){
x.value=x.value.replace(vphone,'($2) $3-$4)');
}
else {
alert('Please enter a valid phone number!');
}
}
// -->
</script>


:D

You can validate the phone number (assuming it's a US/CANADA phone number) against that regular expression, and then remove anything but digits (if you only expect numeric input) with:

mystring = mystring.replace(/\D/,g'')

If it's not a US/CANADA phone number, I leave it up to the honor system and *don't* validate against that regex, since I really don't like paying long distance fees to international numbers, and there are too many different patterns to validate. It just makes form submission easier for everyone. (But people outside the US/CANADA can still submit the form!)

:D

whammy
08-31-2002, 02:15 AM
P.S. to require just 10 digits match the phone number against this:

function tendigits(sPhone){
{
return sPhone.test('/^\d{10}$/');
}

and pass document.formname.phonefieldname.value to the function... i.e:

<form onsubmit = "return tendigits(this.phone.value.replace(/\D/g,''))">

John_Saunders
08-31-2002, 02:19 AM
It's working good! Thanks! Instead of having it print the value that is entered in the form (state abbr. or zip) when an incorrect zip is entered, could you tell me how I could have it say something like: Please enter a valid zip code for "state abbv"?


John

whammy
08-31-2002, 02:20 AM
Just put an alert before the return false that says:

alert('What you want to say here!');

Or if you mean actually WRITE something in the field - u'd do:

onsubmit="if(return ValidZip(this.Zip.value) != true){alert('Bad Monkey!'); return false}">

Not totally sure about that one - I'm no javascript expert... lol and you're getting me into weird syntax here, and I've gotten into too many convulutions of passed parameters in this and other code at the same time - so please forgive me if that one doesn't work... lol ;)

But you get the idea.