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
06-24-2025 06:14 AM
I realize this is quite an old thread, but I ran into a similar issue and wanted to share my solution in case it helps someone else.
When using a regular expression inside a Script Include method that's called via GlideAjax, I found that getParameter() doesn’t return a proper string. This caused issues when trying to apply regex operations.
What worked for me was explicitly casting the parameter like this:
var name = String(this.getParameter("sysparm_name").toString().toLowerCase() || '');
After that, everything behaved as expected.
That said, it’s a pretty odd workaround for something that feels like it should “just work.”