5 Replies Latest reply on Aug 5, 2014 7:37 AM by gkaiseril

    Validating two different zip code formats

    bethg44

      I understand you can go under the Format tab and choose Zip Code or Zip Code +4 but was wondering if there was a javascript validation method that would allow either or within the same text field? Meaning, the text field can allow just a regular zip code or Zip Code + 4. Also no letters allowed as well. I appreciate your help in advance!!!

        • 1. Re: Validating two different zip code formats
          try67 MVP & Adobe Community Professional

          This can be done using JavaScript. For example, you can use this code as the field's custom validation script:

           

          if (event.value) {
              event.rc = /^\d{5}$/.test(event.value) || /^\d{5}-\d{4}$/.test(event.value);
              if (!event.rc) app.alert("Error message goes here");
          }
          
          • 2. Re: Validating two different zip code formats
            bethg44 Level 1

            Thanks Gilad! I appreciate your help entirely!!



            try67 Also I'm gonna sound like a noob (which I am!) could you explain the breakdown of the code? Mainly this: /^\d{5}$/.test(event.value)


            I know what event.value is ...but what do the characters mean before that? I would like to understand better.


            Thanks

            • 3. Re: Validating two different zip code formats
              try67 MVP & Adobe Community Professional

              It's a Regular Expression that tests if the new value is composed of exactly 5 digits.

              • 4. Re: Validating two different zip code formats
                gkaiseril MVP & Adobe Community Professional


                The RegExp is a feature of many programing languages, scripting languages, and other programs. It lets the user describe a general pattern of a data string that can be used to see if a string matches that pattern. There are patterns for date and time, credit card numbers, banking routing transit numbers, social security numbers, etc. One can even check if the individual digits are within the allowed range of values for the sting being tested. It is even possible to use a RegExp string to test each keystroke as someone enters data into a field or split inputted data into a formatted display. And there many more task that can be done using the RegExp.

                 

                The "\d" indicates numeric values only. The "{5}" or "{4}" denotes the length. The "-" indicate the "-" character. The "test" is the method to test the string and return either a logical true if there is match and false when there is not match. The "||" is a logical or so if either test is meet the result is true and when neither test is met the result if false.

                • 5. Re: Validating two different zip code formats
                  gkaiseril MVP & Adobe Community Professional

                  Another variation:

                   

                  /*
                  A breakdown of the ZIP code regular expression follows:
                  /^ # Assert position at the beginning of the string.
                  [0-9]{5} # Match a digit, exactly five times.
                  (?: # Group but don't capture...
                  - # Match a literal "-".
                  [0-9]{4} # Match a digit, exactly four times.
                  ) # End the noncapturing group.
                  ? # Repeat the preceding group between zero and one time.
                  $/ # Assert position at the end of the string.
                  */


                  var RegExp_USZipCode_Valid = /^[0-9]{5}(?:-[0-9]{4})?$/; // US ZipCode 5 and 5+4 formats;
                  if(RegExp_USZipCode_Valid.test(event.value) == false) {
                  app.alert("Not a valid format for US Zip Code or US Zip Code + 4", 1, 0);
                  }

                   

                  It is also possible to write a keystroke validation.