- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on ‎01-16-2023 08:09 AM
There are two field types: "Phone Number" and "Phone Number (E164)" to use phone numbers on the platform.
"Phone Number" is a legacy data type which inherits from String whereas "Phone Number (E164)" is the newer data type that provides richer functionalities and supports phone numbers in multiple country codes. Use the newer data type "Phone Number (E164)" whenever you want to add a custom phone number field in a table.
However, This is specific only for normal fields and not for variables as a similar type does not exist for variables.
To perform a similar validation on catalog variables, I have used undocumented API 'GlideElementPhoneNumber', which i believe utilizes OOB Phone territories (sys_phone_territory) and Phone Validations (sys_phone_validate) tables.
Below solution may not cover all of the scenarios but it seems it covers most of the phone number validations. You can extend this solution to cover your specific use case if needed.
Solution:
1. Create single line text type variable
2. Catalog client script
function onChange(control, oldValue, newValue, isLoading) {
//Type appropriate comment here, and begin script below
if (isLoading)
return;
if (newValue == '') {
g_form.showFieldMsg("phone_number", "Phone number must be provided. Please enter a valid phone number.", "error");
return;
}
var regex = /^[+\d\s]{0,}$/;
if (newValue != '' && !regex.test(newValue)) {
g_form.showFieldMsg("phone_number", "Invalid phone", "error");
} else {
//Call script include
var ga = new GlideAjax('global.ValidatePhoneNumberUtils'); //Scriptinclude
ga.addParam('sysparm_name', 'validatePhoneNumber'); //Method
ga.addParam('fullPh', newValue); //Parameters
ga.getXMLAnswer(getResponse);
function getResponse(response) {
var res = JSON.parse(response);
console.log(res);
var valid = res.valid;
var country = res.country;
if (!valid)
g_form.showFieldMsg("phone_number", "Phone number you entered for " + country + " country is not valid. Please enter number in right format.", "error");
else
g_form.showFieldMsg("phone_number", "You entered a valid phone number for " + country + ".", "info");
}
}
}
3. Client callable script include
var ValidatePhoneNumberUtils = Class.create();
ValidatePhoneNumberUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validatePhoneNumber: function() {
var fullPh = this.getParameter('fullPh');
var obj = {};
var gePN = new GlideElementPhoneNumber();
obj.valid = gePN.setPhoneNumber(fullPh, true);
obj.country = gePN.getTerritory();
obj.globaldisplayValue = gePN.getGlobalDisplayValue();
return JSON.stringify(obj);
},
type: 'ValidatePhoneNumberUtils'
});
Result:
Hope this helps!
- 6,912 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I have tried this and it does not work for me. Can you please re look at this solution?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@BiancaK Thanks for your comment.
I tried it again and it works.
Could you please share what exact issue are you facing?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi,
We tried this, working only for Admins. Not working for normal users from Service Portal.
Please help
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
While creating the Script Include, I was asked to specify a Role for Client Callable Script Include. I selected "Admin" there.
Also, even if I enter the wrong phone number, the Catalog item is still getting submitted.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Made a few additions to this to keep the mandatory valuation working to stop users from submitting a wrong phone number.
function onChange(control, oldValue, newValue, isLoading) {
//Type appropriate comment here, and begin script below
if (isLoading)
return;
if (newValue == '') {
g_form.showFieldMsg("phone_number", "Phone number must be provided. Please enter a valid phone number.", "error");
return;
}
var regex = /^[+\d\s]{0,}$/;
if (newValue != '' && !regex.test(newValue)) {
g_form.showFieldMsg("phone_number", "Invalid phone number", "error");
g_form.setValue('phone_number', '');
} else {
//Call script include
var ga = new GlideAjax('global.ValidatePhoneNumberUtils'); //Scriptinclude
ga.addParam('sysparm_name', 'validatePhoneNumber'); //Method
ga.addParam('fullPh', newValue); //Parameters
ga.getXMLAnswer(getResponse);
function getResponse(response) {
var res = JSON.parse(response);
console.log(res);
var valid = res.valid;
var country = res.country;
if (!valid) {
g_form.showFieldMsg("phone_number", "Phone number you entered for " + country + " country is not valid. Please enter number in right format.", "error");
g_form.setValue('phone_number', ''); }
else {
g_form.showFieldMsg("phone_number", "You entered a valid phone number for " + country + ".", "info");
}
}
}
}
I have added two g_form.SetValue lines to remove the wrong text where the input is incorrect. Typing in a correct phone number will not be removed, just incorrect inputs.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Can you please show how to remove the international country code from the number format for E164 field.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
You can use the attribute in the dictionary
pn_display_territory_selector=false