Strip string field of all non-numeric characters in script include

Chelsea Moore
Tera Guru

I am making a GlideAjax call to check the phone_number on our locations table and ensure that the requestor is not entering any of those number in a field on a catalog item. I had it working if the requestor only enters numeric characters but I need to strip the field on the item of all non-numeric characters if the requestor enters any symbols like -, (, or ). When I added the regex to the script include, I am now just receiving an answer of true no matter what characters I put in. Can someone help me with the script include to extract non-numeric numbers and then query the field on the location table?

Script Include: 

var getPhoneNumber = Class.create();
getPhoneNumber.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
checkDupPhone: function() {
var number = this.getParameter('sysparm_phone');

var phoneRecords = new GlideRecord('cmn_location');

var reg = new SNC.Regex();

var replaceNumber = reg.replace(number,'/[^0-9]/g', '');

phoneRecords.addQuery('phone', replaceNumber);

phoneRecords.query();


if (phoneRecords.next()) {

return true;

}

return false;
},

type: 'getPhoneNumber'
});

 

 

 

Catalog Client Script:

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

// Performing GlideAjax to the 'checkDupPhone' function in the getPhoneNumber script include.
var ga = new GlideAjax('getPhoneNumber');
ga.addParam('sysparm_name', 'checkDupPhone');
ga.addParam('sysparm_phone', g_form.getValue('phone_number'));
ga.getXML(phoneNumber);

function phoneNumber(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

alert("Answer: " + answer); // testing

if (answer == 'true') {
g_form.clearValue('phone_number');
g_form.addErrorMessage("You have entered a CarMax location phone number. Please enter the associate's personal phone number.", 'phone_number');
}

}

}

 

Thank you in advance!

1 ACCEPTED SOLUTION

Chelsea Moore
Tera Guru

The script include wouldn't let me use replace so I ending up checking the regex in the catalog client script to resolve. Using this code: 

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

// Performing GlideAjax to the 'checkDupPhone' function in the getPhoneNumber script include.
var ga = new GlideAjax('getPhoneNumber');
ga.addParam('sysparm_name', 'checkDupPhone');
var number = g_form.getValue('phone_number');
number = number.replace(/\D/g, '');
ga.addParam('sysparm_phone', number);
ga.getXML(phoneNumber);

function phoneNumber(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

alert("Answer: " + answer); // testing

if (answer == 'true') {

g_form.clearValue('phone_number');
g_form.addErrorMessage("You have entered a CarMax location phone number. Please enter the associate's personal phone number.", 'phone_number');
}

}

}

View solution in original post

4 REPLIES 4

Saurav11
Kilo Patron
Kilo Patron

Hello,

Please check the below thread for your regex answer

https://community.servicenow.com/community?id=community_question&sys_id=793e47eddb9cdbc01dcaf3231f9619e5

 

Please mark answer correct/helpful based on Impact.

Thanks.

shloke04
Kilo Patron

Hi,

Your code looks okay to me. You just need to replace the line where you are checking for Regex in your Script include.

Use the line below:

var replaceNumber = number.replace(/\D/g,'');

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Sushma R1
Tera Expert

Script looks almost ok, just that you need to change the regex try your regex in regex101. Example this might trim your (- + space etc .replace(/^(\+)|\D/g use $1 or \1

 

https://stackoverflow.com/questions/28607395/regex-to-remove-chars-from-phone-numbers

 

Hit helpful if it was 🙂

Chelsea Moore
Tera Guru

The script include wouldn't let me use replace so I ending up checking the regex in the catalog client script to resolve. Using this code: 

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

// Performing GlideAjax to the 'checkDupPhone' function in the getPhoneNumber script include.
var ga = new GlideAjax('getPhoneNumber');
ga.addParam('sysparm_name', 'checkDupPhone');
var number = g_form.getValue('phone_number');
number = number.replace(/\D/g, '');
ga.addParam('sysparm_phone', number);
ga.getXML(phoneNumber);

function phoneNumber(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

alert("Answer: " + answer); // testing

if (answer == 'true') {

g_form.clearValue('phone_number');
g_form.addErrorMessage("You have entered a CarMax location phone number. Please enter the associate's personal phone number.", 'phone_number');
}

}

}