15 Replies Latest reply on Aug 24, 2015 10:08 AM by ahlaj77

    Need Help with Regular Expression (RegEx)

    ahlaj77

      Trying to wrap my head around a Regular Expression for the following format example: 0022-C-4452       OR     0022-C-4452-C


      *The first 4 digits are always numbers

      *The Last 5 digits are alpha numeric

      *Last digit (if used) is always "C" (referring to the second format)

       

      Would like the dashes to "auto populate" if possible? Would this go into the Custom Format? Keystroke? Or Validation? I appreciate any help!

        • 1. Re: Need Help with Regular Expression (RegEx)
          Test Screen Name Most Valuable Participant

          Take great care with automatic population of characters, if not done with great sensitivity it can really mess up the form filler.

           

          Consider: if you just say "I see 4 digits were typed, I'll add a dash", if the user is not entirely tuned, they will type their own dash. They then end up either seeing both, and painstakingly correcting, or leaving a field with some double dashes. As a form filler I loathe all attempts to guess what I wanted to do.

          • 2. Re: Need Help with Regular Expression (RegEx)
            gkaiseril MVP & Adobe Community Professional

            Pretty hard to figure out the image for the RegExp from on example and only a comment that the first 4 characters are always numbers. Does that mean we assume the next four characters are any character and the last character can be any character?

             

            I would start out with solving the issue for 1 of the 2 types of strings and then work up the other.

             

            var RE_MyCode0 = /^(\d{4})[- .]{0,1}(\w{4})$/; // 4 digits optional separator of "-, space, or .", any 4 alphanumeric characters;
            console.println("Test RE_MyCode0 result: " + RE_MyCode0.test(cString))
            if(RE_MyCode0.test(cString) == true) {

            // use returned parts of RegExp to break the value into strngs and add separator;

            console.println(RegExp.$1 + "-" + RegExp.$2);
            }

            The next string:

             

            var RE_MyCode1 = /^(\d{4})[- .]{0,1}(\w{4})[- .]{0,1}(\w{1})$/;
            console.println(RE_MyCode1.test(cString));
            if(RE_MyCode1.test(cString) == true) {
            console.println(RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3);
            }

            • 3. Re: Need Help with Regular Expression (RegEx)
              ahlaj77 Level 1

              Hi! Not sure if my edits (to my original message) came through slowly but the rest of my message said:

               

              *The first 4 digits are always numbers

              *The Last 5 digits are alpha numeric

              *Last digit (if used) is always "C" (referring to the second format)



              • 4. Re: Need Help with Regular Expression (RegEx)
                gkaiseril MVP & Adobe Community Professional

                I still think you have not properly described your problem.

                 

                1st 4 characters digits.

                Optional separator.

                Next 6 characters alphanumeric and "-".

                 

                OR

                 

                1st 4 characters digits.

                Optional separator.

                Next 6 characters alphanumeric and "-".

                Optional separator.

                Character "C"

                 

                Note that the "-" is not a digit and not a alphabetic character. It is a white space character.

                 

                Try:

                 

                function MyRe (cString){
                var cFormatted = "";
                var RE_MyCode0 = /^(\d{4})[- .]{0,1}([A-Za-a0-9-]{6})$/;
                var RE_MyCode1 = /^(\d{4})[- .]{0,1}([A-Za-a0-9-]{6})[- .]{0,1}([C])$/;
                if(RE_MyCode0.test(cString) == true) {
                cFormatted=  RegExp.$1 + "-" + RegExp.$2;
                }
                if(RE_MyCode1.test(cString) == true) {
                cFormatted = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3;
                }
                return cFormatted;
                } // end MyRe function;

                // some testing;
                var MyString = "0022-C-4452"; // good string;
                console.println("Input: " + MyString);
                console.println("Result: " + MyRe(MyString) );

                var MyString = "0022-C-4452-C"; // goo string;
                console.println("Input: " + MyString);
                console.println("Result: " + MyRe(MyString) );

                var MyString = "A022-C-4452" // bad string;
                console.println("Input: " + MyString);
                console.println("Result: " + MyRe(MyString) );

                var MyString = "0022-C-4452-Z" // bad string;
                console.println("Input: " + MyString);
                console.println("Result: " + MyRe(MyString) );

                • 5. Re: Need Help with Regular Expression (RegEx)
                  ahlaj77 Level 1

                  Yes you are correct! Sorry for the confusion! Yes 6 total including the "-". The dash would already be there but the characters surrounding it are alphanumeric (as you mentioned in your description).

                   

                  Is this function for a document level javascript and then I place MyRe(event.value) into the Calculation Script of the textfield?

                  • 6. Re: Need Help with Regular Expression (RegEx)
                    gkaiseril MVP & Adobe Community Professional

                    It could go in either place. The document level lets one reuse it anywhere in the form. If one places it in the calculation field for one field, then that field has to be used in a calculation before the function would be available for other fields.


                    • 7. Re: Need Help with Regular Expression (RegEx)
                      ahlaj77 Level 1

                      Here are my screenshots I placed it into the Document Level Javascript, then call it in the Stock Number textfield in the Custom Calculation script area, and my bottom image shows I can just still type whatever I want into the field.

                      Screenshots.jpg

                      • 8. Re: Need Help with Regular Expression (RegEx)
                        gkaiseril MVP & Adobe Community Professional

                        And with the supplied stock number not meeting your required format it will not format the result as you want it.

                         

                        The function is more like a custom r formatting script but you would need to add some script for an error message and setting the event return code. You may need to modify the code for the validation code for setting the event.rc value and an error message.

                         

                        You asked for the RegExp for the examples provided.

                         

                        For the stock number entered the script will not format the string because it does not fit either example format.

                         

                        What happened when you enter the provided strings?

                        • 9. Re: Need Help with Regular Expression (RegEx)
                          ahlaj77 Level 1

                          Thanks for your help so far!!! I appreciate it!!!

                          Okay. I have this.

                                if(event.value.match(/^(\d{4})[- .]{0,1}([A-Za-a0-9-]{6})$/) == null || event.value.match(/^(\d{4})[- .]{0,1}([A-Za-a0-9-]{6})[- .]{0,1}([C])$/) == null)

                                {

                                      app.alert('That was not valid stock number');

                                      event.rc = false;

                                }

                               else

                               {

                                  event.rc = true;

                                }

                           

                          When I put in 0022-C-4452 (good string) it throws an error. If I try 0022-B-4452-C (good string..notice I changed the C to a B..beside 0022) it throws an error.
                          Here is my document:

                          https://www.dropbox.com/s/axrd950exzakwgi/Stock%20Number.pdf?dl=0

                          • 10. Re: Need Help with Regular Expression (RegEx)
                            try67 MVP & Adobe Community Professional

                            You have a logical error in your statement, I believe. You're basically saying that if either condition is incorrect then show the error message. The problem is that any string that matches the second condition can't match the first one, because they're mutually exclusive. To fix it I think all you have to do is drop the $ symbol at the end of the first RegExp. That would make the second condition become a sub-set of the first one, and your validation will be successful.

                            • 11. Re: Need Help with Regular Expression (RegEx)
                              ahlaj77 Level 1

                              I have this code in my "Run Custom Validation Script" area and I dropped the $ sign in the FIRST code and its still yelling at me I provided my document above (in the link) to show where my code was and how it was set up. Trying to help out a Client :-/ This is the last thing I have to do and its driving me crazy lol

                              • 12. Re: Need Help with Regular Expression (RegEx)
                                try67 MVP & Adobe Community Professional

                                Change the OR operator (||) to AND (&&).

                                • 13. Re: Need Help with Regular Expression (RegEx)
                                  ahlaj77 Level 1

                                  Thanks for that! It worked

                                   

                                  One last thing, if I were to type in 0022-B-4452 as 0022B4452 in the textfield (without dashes), is there a way it would automatically change to 0022-B-4452?

                                  • 14. Re: Need Help with Regular Expression (RegEx)
                                    try67 MVP & Adobe Community Professional

                                    Yes, you could use a custom format script to change the way the input text looks, or a custom validation script to change the actual value itself.

                                    • 15. Re: Need Help with Regular Expression (RegEx)
                                      gkaiseril MVP & Adobe Community Professional

                                      Stock Number Example has the validation for both formats with or with separators of "-", space, or" .".

                                       

                                      Only the displayed value is be adjusted. The imputed value will be what the user entered.

                                      • 16. Re: Need Help with Regular Expression (RegEx)
                                        ahlaj77 Level 1

                                        Thank you for that! It really helped me out!! I appreciate your help and the time you took to help me out!