5 Replies Latest reply on May 1, 2014 9:34 AM by Qloud

    Script phone number plus extension

    Qloud Level 1

      I have a variety of phone numbers that could be entered into one field. 10, 13, or 14 digits. I'm trying to write a script to accomodate this. I would like the user to be able to put in numbers or number+characters but I want the results to be as follows

      (123) 123-1234 or (123) 123-1234 x123 or (123) 123-1234 x1234 depending on the number of "numbers" entered.

       

      Any help with this is much appreciated.

        • 1. Re: Script phone number plus extension
          Qloud Level 1

          This is the script I tried to get it working. I'm new to this so something in the syntax of the last part is wrong.

           

          var re_NA_Phone_Commit = /^\(?([0-9]{3})\)?[. ]?([0-9]{3})[-. ]?([0-9]{4})?[x]?([0-9]{4})$/

           

          if (re_NA_Phone_Commit.test(event.value))

           

          event.value = "("+ RegExp.$1 +") " + RegExp.$2 + "-" + ReExp.$3 + "x" + "  + RegExp.$4;

           

          else {

           

          event.value = "";

           

          event.rc = false;

           

          }

           

          If I take out the underlined part everything works fine. I also tried "{<5}" where the last "{4}" is in line one

          • 2. Re: Script phone number plus extension
            gkaiseril MVP & Adobe Community Professional

            If you are going to have either a phone number without extenston or a phone number with an exttension, you need to provide 2 RegExp expressions, one for each type and then test for each type of format and perfrom the desired formatting for the type of phone number.

            • 3. Re: Script phone number plus extension
              Qloud Level 1

              Ok, so how would that look in syntax? Would I have a 2nd event.value? or another line that mirrors the regexp line but it's 2regexp and 3rexexp?

               

              event.value = "(+ 2RegExp.$1 +")...... <---- like that?

               

              • 4. Re: Script phone number plus extension
                gkaiseril MVP & Adobe Community Professional

                No.

                 

                You need some if statements:

                 

                if(event.value) {

                // phone number with 3 or 4 digit extension;

                var re_NA_Phone_Commit0 = /^\(?([0-9]{3})\)?[-. ]([0-9]{3})[-. ]?([0-9]{4})[x]?([0-9]{3,4})$/

                // phone number with no extension;

                var re_NA_Phone_Commit1 = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/

                event.rc = false;

                if (re_NA_Phone_Commit0.test(event.value)) {

                event.value = "(" + RegExp.$1 +") " + RegExp.$2 + "-" + RegExp.$3 + "x" + RegExp.$4;

                event.rc = true;

                } else if (re_NA_Phone_Commit1.test(event.value)) {

                event.value = "(" + RegExp.$1 + ") " + RegExp.$2 + "-" + RegExp.$3;

                event.rc = true;

                } // end RegExp test;

                } // end if event.value not empty;

                 

                Or use the switch statement:

                 

                if(event.value) {

                // phone number with 3 or 4 digit extension;

                var re_NA_Phone_Commit0 = /^\(?([0-9]{3})\)?[-. ]([0-9]{3})[-. ]?([0-9]{4})[x]?([0-9]{3,4})$/

                // phone number with no extension;

                var re_NA_Phone_Commit1 = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/

                switch(true) {

                case (re_NA_Phone_Commit0.test(event.value)) :

                event.value = "(" + RegExp.$1 +") " + RegExp.$2 + "-" + RegExp.$3 + "x" + RegExp.$4;

                event.rc = true;

                break;

                case (re_NA_Phone_Commit1.test(event.value)) :

                event.value = "(" + RegExp.$1 + ") " + RegExp.$2 + "-" + RegExp.$3;

                event.rc = true;

                break;

                default:

                event.rc = false;

                break;

                } // end switch;

                } // if event.value not null;

                • 5. Re: Script phone number plus extension
                  Qloud Level 1

                  Thanks, the first one worked like a champ and I'll check out the second when I get a little more time.