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

nope...that didn't work either.

I just occured to me to use a Try...Catch so I can get the error message.

This is what I get for the .replace line:

 

The choice of Java method java.lang.String.replace matching JavaScript argument types (function,string) is ambiguous; candidate methods are: class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence) class java.lang.String replace(char,char) (sys_script_include.44f589510f0113002fed8e8ce1050e03.script; line 9)

 

Hi, I have made a field called phone number which is of string type and made an on change client script in that field. Could you please try this. Hope this will help you out. It will force you to only enter digits not even string.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var val = g_form.getValue('u_number');
alert(val.length);
var patt = /\d{10}/;
if (val.length ==10)
{
var result = patt.test(val);
if (!result) {
g_form.setValue('u_number','');
alert('Please enter 10 digit phone number');
return;
}
}
else
{
g_form.setValue('u_number','');
alert('lease enter 10 digit phone number');
return;
}
}

if the response is useful, mark the post as correct and helpful.

 

Thanks.

Todd_Goodhew
Kilo Guru

Eureka!!! Problem solved.

Did a quick google search on the java method error above.  In short it doesn't know if it is a Java string or a JavaScript string to it bombs out.

solution is to force it to a Javascript string.

var phoneNumber = this.getParameter('sysparm_phone_number') + '';

 

I've seen this is code from others, but never until now understood its value.

 

Thanks to all for the help today.

 

 

This has solved my issue! Thanks.

solved my own condition 🙂