Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to format Phone Number variable

Teri Bobst
Mega Guru

I have a variable in our mobile service workflow for our MDM to record the number of the new service. I also have a business rule that will then populate the mobile number field on the user record when this task is closed. The problem is that it populates that field exactly as it is entered (even though the type on that field is phone number and if you just type it in there it will format correctly).

So, I need a way either in my variable, business rule, or elsewhere to maintain the phone number format (xxx) xxx-xxxx even when xxxxxxxxxx or xxx xxx xxxx is entered in the variable.

Does anyway know of a way to handle this? I will include my business rule below:

function onBefore(current, previous) {

    //This function will be automatically called when this rule is processed.

    var gr = new GlideRecord('sys_user');

  gr.addQuery('sys_id', current.request.requested_for);

  gr.query();

  if (gr.next()){

  gs.addInfoMessage("SUCCESS: Found User!");

  gs.addInfoMessage("Mobile # is: " + current.variable_pool.mobile_number);

  gr.mobile_phone = current.variable_pool.mobile_number;

  gr.update();

  } else {

  gs.addInfoMessage("ERROR: Could not find user record: " + current.request.requested_for.name);

  }

}

Thanks,

Teri

1 ACCEPTED SOLUTION

TJW2
Mega Guru

The following will format a string input as xxxxxxxxxx into (xxx)xxx-xxxx



var inputPhone = current.variables.mobile_number;


var newPhone = '('+inputPhone.substr(0,3)+')'+inputPhone.substr(3,3)+'-'+inputPhone.substr(6,4);



On the catalog item I would verify the input field is 10 long and numeric using an OnChange client script:



if( (newValue != "") && (/[0-9]{10}/i.exec(newValue) > -1) ) {


      alert("The phone number must be in this format: 1234567890.\n");


      g_form.setValue('field_name','');


}


View solution in original post

9 REPLIES 9

lisac
Giga Contributor

Terri - where are you putting this script?   Lisa Callaghan (Ex Scripps)


I tried this but it did not work for me:



alert('I called the script with ' + newValue);

     


      if( (newValue != "") && (/[0-9]{10}/i.exec(newValue) > -1) ) {


    alert('The phone number must be in this format: 1234567890.');
    g_form.setValue('u_phone_number','');

      } else {


    alert('Formatting');
    var inputPhone = g_form.getValue('u_phone_number');
    var newPhone = '('+inputPhone.substr(0,3)+')'+inputPhone.substr(3,3)+'-'+inputPhone.substr(6,4);

      }



It never would validate that the phone number was 1234567890


samwallace881
Giga Expert

This client script should work well for your needs:


If the user types in 10 digits, this code will format it to look like (xxx) xxx-xxxx.


If the user types in their phone number in the correct format, then nothing will happen, otherwise, it will change it to the correct format or throw an error.


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


                          if (isLoading || newValue === '') {


                                                        return;


                          }


                       


                          g_form.hideFieldMsg('phone_number');


                          var pattern = /^\(\d{3}\)\s\d{3}-\d{4}$/; //(xxx) xxx-xxxx


                          var phone = g_form.getValue('phone_number');


                       


                          if(!pattern.test(phone)){


                                                        phone = phone.replace(/\D/g,'');


                                                        var regex = /^\d{10}$/;


                                                        var is_valid = regex.test(phone);


                                                        if(!is_valid){


                                                                                    g_form.clearValue('phone_number');


                                                                                    g_form.showFieldMsg('phone_number', "Please enter 10 digits", 'error');


                                                        }else{


                                                                                    phone = '(' + phone.slice(0,3) + ') ' + phone.slice(3,6)+'-' + phone.slice(6,10);


                                                                                    g_form.setValue('phone_number', phone);


                                                                                    g_form.showFieldMsg('phone_number', "Phone number formatted", 'info');


                                                        }


                          }


                       


                       


}


Community Alums
Not applicable

The originally accepted solution did not work for me, however this one did and should be the 'Accepted Solution' for this question.

Sandeep Reddy L
Tera Contributor

Hi Guys,

 

when i tried this script it was working perfectly but, I want to do validation over mobile number with the format when ever the form is submitted.

can any one suggest me how to check format of mobile in Onsubmit Client scripts.

 

Thanks,

Sandeep Reddy Lebaka