Skip to main content
Participating Frequently
March 9, 2020
Question

Complex validation of a 5-digit US Zip Code in an electronic Adobe form.

  • March 9, 2020
  • 1 reply
  • 2778 views

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!

    This topic has been closed for replies.

    1 reply

    Thom Parker
    Community Expert
    Community Expert
    March 10, 2020

    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;

    }

     

    Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
    Participating Frequently
    March 11, 2020

    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 = ""

     

     

    Participating Frequently
    March 11, 2020

    Thom,

     

    My apologies. Line 3 should read as:

    if (zip != "") {