6 Replies Latest reply on Nov 30, 2015 5:33 PM by RedSamurai

    field validation w/RegExp question. . .

    RedSamurai

      I'm trying to use this to validate a field entry. It works, except it doesn't seem to recognize the pattern when it's correct. I know it's wrong, but where?

       

      Thanks.

       

      var fieldText = this.getField(event);

      var testPattern = /\d{4}-\d{4}/;

       

       

      if (testPattern.test(fieldText)) {

        event.rc = true;

      }

       

      else {app.alert('msg');

        event.rc = false;

      }0-

        • 1. Re: field validation w/RegExp question. . .
          George_Johnson MVP & Adobe Community Professional

          Where did you place this code and what is the pattern you want to test, in words, not code? I ask because I suspect the pattern you have isn't specific enough for four digits followed by a hyphen followed by four more digits, and nothing more.

          • 2. Re: field validation w/RegExp question. . .
            Karl Heinz Kremer Adobe Community Professional

            Your first line is wrong. I assume you are using this as a custom validation script. In that case, to get the string to validate, you would just use event.value. However, your script will also validate e.g. 123456-123456 as correct, because it does contain the pattern 3456-1234, which is correctly matched by your regular expression. You will need to make the RE more restrictive. You also want to make sure that a completely empty field also results in a valid entry (otherwise somebody will have a hard time changing an incorrect value:

             

             

            event.rc = true;    // that's the default
            
            if (event.value != "") {
                var testPattern = /^\d{4}-\d{4}$/;
                if (testPattern.test(event.value)) {
                    event.rc = true;
                }
                else {
                    app.alert('msg')
                    event.rc = false;
                }
            }
            

             

            You can simplify this code because the default behavior is a valid input value and only test for the case when the pattern is not matched.

            • 3. Re: field validation w/RegExp question. . .
              gkaiseril MVP & Adobe Community Professional

              With strict formatting:

               


              // custom validation script;
              // RegExp for strict 4 digits, dash, 4 digits;
              var testPattern = /^(\d{4})(-)(\d{4})$/;
              // test for value not matching the RegExp;
              // validation assumes the result is true unless set otherwise;
              // do not process and empty entry;
              if(event.value != "" && testPattern.test(event.value) == false) {
              // set event rc to false;
              event.rc = false;
              // display error message;
              app.alert("Enter must be four digits, \"-\", and 4 digits", 0, 0);
              }
              // end custom validation script;

               

              For allowing the omission of the "-", not using any separator or using a ".", "/", or space in place of the "-" and changing the field value to strict format:


              // custom validation script;
              // RegExp for 4 digits, optional dash, ".", space or nothng, 4 digits;
              var testPattern = /^(\d{4})[-. ]{0,1}(\d{4})$/;
              // test for value not matching the RegExp;
              // validation assumes the result is true unless set otherwise;
              // do not process and empty entry;
              if(event.value != "" && testPattern.test(event.value) == false) {
              // set event rc to false;
              event.rc = false;
              // display error message;
              app.alert("Enter must be four digits, \"-\", and 4 digits", 0, 0);
              } else {
              event.value = RegExp.$1 + "-" + RegExp.$2;
              }
              // end custom validation script;

              • 4. Re: field validation w/RegExp question. . .
                RedSamurai Level 1

                Sorry it was the first time I used the forum here. . . my mistake. I did load the script and was helped. My apologies for the mistake. . .

                 

                Thanks.

                 

                . . .

                • 5. Re: field validation w/RegExp question. . .
                  RedSamurai Level 1

                  I appreciate the help. . .

                  • 6. Re: field validation w/RegExp question. . .
                    RedSamurai Level 1

                    I was amazed with the quick responses.

                     

                    Thanks you everyone.

                     

                    . . .