Copy link to clipboard
Copied
Good afternoon everyone!
I am trying to go beyond the basic "Format 'Special' Zip Code" entry validation of Adobe electronic form fields.
What I would like to do is to validate the user entered Zip Code against valid ranges for the state that is (user) entered in the State field.
I have the USPS list of Zip Code ranges by state/region, and have already populated my 'State' dropdown field with the list of 2-digit state abbreviations.
What I am looking for is an 'If/Then' approach so that if TX is entered in the State field, then the Zip Code entered must be within one of the ranges of: 73301-73301 / 75001-75501 / 75503-79999 / 88510-88589.
Now, I am no JavaScript guru so please let me know if this will be too complex to be effective.
I would appreciate any help or suggestions y'all could throw my way.
Thank you!
Copy link to clipboard
Copied
This is totally doable. What you need to do is organize your zip code range data so that it is accessible by the state abbreviation. Use a generic JS object like this.
oZipRanges = {"TX":[[73301,73301],[ [75001,75501],[75503,79999]],
"AL":[[73301,73301],[ [75001,75501],[75503,79999]],
...etc...
};
Now use the validation script on the zip entry to check against this list
var zipRange = null;
if(zipRange = oZipRanges[this.getField("State").value])
{
.. Test against range...
}
else
{
app.alert("State must be selected first");
event.rc = false;
}
Copy link to clipboard
Copied
Thom,
Thank you, very much! The only thing missing is a error message for invalid entries. As a Technical Writer, I am not exactly a priority to 'JavaScript' savy folks at my company, but a colleauge of mine actually came up with this solution for the Zip field:
var state = this.getField("State").value;
var zip = event.value;
if zip = event.value;
var dataTable = [["AK",99501,99950], ["AL",35004,36925], ["AR",71601,72959], ["AR",75502,75502], ["AZ",85001,86556], ["CA",90001,96162], ["CO",80001,81658], ["CT",6001,6389], ["CT",6401,6928], ["DC",20001,20039], ["DC",20042,20599], ["DC",20799,20799], ["DE",19701,19980], ["FL",32004,34997], ["GA",30001,31999], ["GA",39901,39901], ["HI",96701,96898], ["IA",50001,52809], ["IA",68119,68120], ["ID",83201,83876], ["IL",60001,62999], ["IN",46001,47997], ["KS",66002,67954], ["KY",40003,42788], ["LA",70001,71232], ["LA",71234,71497], ["MA",1001,2791], ["MA",5501,5544], ["MD",20331,20331], ["MD",20335,20797], ["MD",20812,21930], ["ME",3901,4992], ["MI",48001,49971], ["MN",55001,56763], ["MS",38601,39776], ["MS",71233,71233], ["MT",59001,59937], ["NC",27006,28909], ["ND",58001,58856], ["NE",68001,68118], ["NE",68122,69367], ["NH",3031,3897], ["NJ",7001,8989], ["NM",87001,88441], ["NV",88901,89883], ["NY",6390,6390], ["NY",10001,14975], ["OH",43001,45999], ["OK",73001,73199], ["OK",73401,74966], ["OR",97001,97920], ["PA",15001,19640], ["RI",2801,2940], ["SC",29001,29948], ["SD",57001,57799], ["TN",37010,38589], ["TX",73301,73301], ["TX",75001,75501], ["TX",75503,79999], ["TX",88510,88589], ["UT",84001,84784], ["VA",20040,20041], ["VA",20040,20167], ["VA",20042,20042], ["VA",22001,24658], ["VT",5001,5495], ["VT",5601,5907], ["WA",98001,99403], ["WI",53001,54990], ["WV",24701,26886], ["WY",82001,83128]];
var valid = false;
dataTable.forEach(validate);
function validate(item, index) {
if(item[0]==state)
{
if(zip>=item[1] && zip<=item[2])
{
valid = true;
}
}
}
if(!valid)
{
app.alert({cMsg:"This is not a valid Zip Code ("+zip+") for "+state+".",nIcon: 0, nType: 1,cTitle:"Invalid Input"});
event.value = "";
}
}
Including this for the State field:
this.getField("Zip").value = ""
Copy link to clipboard
Copied
Thom,
My apologies. Line 3 should read as:
if (zip != "") {
Copy link to clipboard
Copied
Was there a question? And why not not use the structure I provided?
The code you posted is very inefficient.
Copy link to clipboard
Copied
Thom,
Yes, there was a question. I did not notice my first reply cut it off!
How/Where do I add the error message (in the code I sent) into the code you sent? Because I agree, what I posted is rather clunky.
And, I added your code (with the full Zip Code list) and keep getting an error message: SyntaxError: syntax error 61: at line 62
I am sure that I messed the code up with my addition of the full Zip Code list because I was not sure of the proper deployment of the '[ ]' symbols. But, here is what I did.
oZipRanges = {
"AK":[[99501,99950]],
"AL":[[35004,36925]],
"AR":[[71601,72959],[75502,75502]],
"AZ":[[85001,86556]],
"CA":[[90001,96162]],
"CO":[[80001,81658]],
"CT":[[06001,06389],[06401,06928]],
"DC":[[20001,20039],[20042,20599],[20799,20799]],
"DE":[[19701,19980]],
"FL":[[32004,34997]],
"GA":[[30001,31999],[39901,39901]],
"HI":[[96701,96898]],
"IA":[[50001,52809],[68119,68120]],
"ID":[[83201,83876]],
"IL":[[60001,62999]],
"IN":[[46001,47997]],
"KS":[[66002,67954]],
"KY":[[40003,42788]],
"LA":[[70001,71232],[71234,71497]],
"MA":[[01001,02791]],
"MA":[[05501,05544]],
"MD":[[20331,20331]],
"MD":[[20335,20797],[20812,21930]],
"ME":[[03901,04992]],
"MI":[[48001,49971]],
"MN":[[55001,56763]],
"MS":[[38601,39776],[71233,71233]],
"MT":[[59001,59937]],
"NC":[[27006,28909]],
"ND":[[58001,58856]],
"NE":[[68001,68118],[68122,69367]],
"NH":[[03031,03897]],
"NJ":[[07001,08989]],
"NM":[[87001,88441]],
"NV":[[88901,89883]],
"NY":[[06390,06390],[10001,14975]],
"OH":[[43001,45999]],
"OK":[[73001,73199],[73401,74966]],
"OR":[[97001,97920]],
"PA":[[15001,19640]],
"RI":[[02801,02940]],
"SC":[[29001,29948]],
"SD":[[57001,57799]],
"TN":[[37010,38589]],
"TX":[[73301,73301],[75001,75501],[75503,79999],[88510,88589]],
"UT":[[84001,84784]],
"VA":[[20040,20041]],
"VA":[[20040,20167],[20042,20042],[22001,24658]],
"VT":[[05001,05495],[05601,05907]],
"WA":[[98001,99403]],
"WI":[[53001,54990]],
"WV":[[24701,26886]],
"WY":[[82001,83128]]
};
var zipRange = null;
if(zipRange = oZipRanges[this.getField("State").value])
{
... Test against range...
}
else
{
app.alert("State must be selected first");
event.rc = false;
}
Copy link to clipboard
Copied
Thom,
It is weird, the debugger keeps highlighting the '{' immediately following 'else.'
It has to be something I did wrong, but I can not find it to save my life.
Copy link to clipboard
Copied
So what is line 61 and line 62?
Code is very picky about syntax. Each opening bracket must match a closing bracket and they have to be perfectly nested. All elements in a list are separated with a ",". Stuff like this. Somethimes it's insanely difficult to figure out, and it will be right in front of you the whole time.
I'm happy to help, but you have to give me the exact and complete info. What is on line 61 and 62, what is the exact error message?
Copy link to clipboard
Copied
Also, you do know that
... Test against range...
is not real code? Its a placeholder for what you need to add. And it will cause a syntax error.
Copy link to clipboard
Copied
Wow. I am even worse at this than I thought. Well, yes - I thought it looked like a place holder, but I figured it needed to be there and the code would populate it with something! LOL
Did I tell you I am a Technical Writer and not a coder?! LOL Well, it is pretty obvious now that no matter what I am - a coder is not it.
Ok, so what do I need to populate that section with? If I am guessing right, it needs to be a reference back to the 'ZipRanges' section. But it also needs to reference what is in the State field first?
The error is highlighting the final bracket in your code.
Copy link to clipboard
Copied
You had it right on the code you're co-worker helped with. The code needs to loop over the valid zip ranges to find out if the zip fits into one of them. Really, that other code works almost as well. Its slightly less efficient because the array has to be searched for the state, whereas the object notation provides the range for the state without a search.
Here's the code for a validation script
var zip = event.value;
if(zipRange = oZipRanges[this.getField("State").value])
{
if(!(event.rc = zipRange.some(function(a){return (zip>=a[0] && (zip<=a[1]);})))
app.alert("This is not a valid Zip Code ("+zip+") for "+state+".",0,1,"Invalid Input");
else
{
app.alert("State must be selected first");
event.rc = false;
}
Copy link to clipboard
Copied
This is very interesting to me.
I've tried to read through all the threads and "piece" the script together, but there were so many changes and corrections, I was wondering, does anyone have a copy of the final, "correct" script?
Copy link to clipboard
Copied
This is very interesting to me.
I've tried to read through all the threads and "piece" the script together, but there were so many changes and corrections, I was wondering, does anyone have a copy of the final, "correct" script?