Script Include and Regex Issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2018 01:24 PM
I have a need to build a script include that formats a phone number that is passed to it via a client script.
I have a couple of spots in the script that are not working correctly...the regex replace and the length check.
The regex check doesn't work. If I put quotes around it, then it will get passed that line, but then the check doesn't remove non-digits from the string.
********************************************** Catalog Client Script **********************************************
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.addErrorMessage("in onchange client script");
var altPhoneNumber = g_form.getValue('u_alternate_phone');
alert("phone=" + altPhoneNumber);
var ga = new GlideAjax('u_FormatPhoneNumber');
ga.addParam('sysparm_name','formatPhoneNumber');
ga.addParam('sysparm_phone_number',altPhoneNumber);
ga.getXML(callbackfunction);
function callbackfunction(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//*****
// this is causing a loop
//g_form.setValue("u_alternate_phone",answer);
}
}
********************************************** Script Include ************************************************
var u_FormatPhoneNumber = Class.create();
u_FormatPhoneNumber.prototype = Object.extendsObject(AbstractAjaxProcessor, {
formatPhoneNumber:function(){
var phoneNumber = this.getParameter('sysparm_phone_number').toString();
var digitOnlyRE = /\D/g;
var phoneNumberClean = phoneNumber.replace(digitOnlyRE,''); // strip non-digit characters
var badNumber = false;
var phoneNumberFormatted = '';
gs.addInfoMessage("phone clean: " + phoneNumberClean);
if (phoneNumberClean.length > 10) {
badNumber = "it worked";
}
if (phoneNumberClean.length == 11 && phoneNumberClean[0] == '1'){
phoneNumberClean = phoneNumberClean.substring(1);
} else if (phoneNumberClean.length == 11 && phoneNumberClean[0] != '1'){
badNumber = true;
} else if (phoneNumberClean.length <10 || phoneNumberClean.length > 11) {
badNumber = true;
}
gs.addInfoMessage("badNumber=" + badNumber);
if (badNumber == false){
// put into final format
phoneNumberFormatted = "(" + phoneNumberClean.substring(0,3) + ") " + phoneNumberClean.substring(3,6) + "-" + phoneNumberClean.substring(6);
//g_form.setValue("u_alternate_phone",phoneNumberFormatted);
gs.addInfoMessage("formatted number=" + phoneNumberFormatted);
} else {
gs.addInfoMessage('Phone enter a 10 digit phone number','error');
}
return phoneNumberFormatted;
},
type: 'u_FormatPhoneNumber'
});
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2018 01:54 PM
Hi Todd,
I ran the following code in a "Scripts - Background" window and it worked just fine. From the script-background, it looks like your code is working well. Are you stepping through the code in the debugger and its not working? To be more specific, how are you certain those parts of the code are not working?
Here is the script ran under "Scripts - Background" that worked for me based very closely off of your code.
gs.info("This is the start of the script");
var phoneNumber = "1.909.555.1234";
var digitOnlyRE = /\D/g;
var phoneNumberClean = phoneNumber.replace(digitOnlyRE,''); // strip non-digit characters
gs.info(typeof phoneNumberClean);
gs.info(phoneNumberClean);
gs.info("This is the end of the script");
~Constantine

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2018 02:45 PM
Can you try like below.
var phoneNumber = this.getParameter('sysparm_phone_number');
var phoneNumberClean = phoneNumber.toString().replace(/\D/g,'');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2018 03:52 PM
replying to several posts in this one. Roll with me.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2018 06:35 PM
doesn't look like any issue with the code, can we try like
var phoneNumber = this.getParameter('sysparm_phone_number').toString();
var phoneNumberClean = phoneNumber.replace(/[^0-9]/g,'');