Script Include and Regex Issue

Todd_Goodhew
Kilo Guru

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'
});

 

10 REPLIES 10

Constantine Kr1
Giga Guru

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

Shishir Srivast
Mega Sage

Can you try like below.

 

var phoneNumber = this.getParameter('sysparm_phone_number');
var phoneNumberClean = phoneNumber.toString().replace(/\D/g,'');

Todd_Goodhew
Kilo Guru

replying to several posts in this one.  Roll with me.

 

constantinekrick:
I had the same experience.  running it via Scripts - Background works just fine.
I know it is failing as the addMessage line after the var declarations never fire.  If I comment out the line that is doing the replace, then the addMessage fires.
As for Script Debugger, it does nothing for me.  I put in break points, say on the first var declaration, and it never stops...just runs all the way through.
 
Shishir:
That didn't work either.  The line above for this.getParameter has a .toString() and the end.  I even removed the .toString on that line for your test...no joy.
 
 

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,'');