- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2016 07:08 AM
I'm not seeing a phone number field type for user input. Does ServiceNow provide any kind of cell phone validator in the Service Catalog, or will I need to write a client script to do this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2016 07:16 AM
Brad has a useful thread from a couple years ago.
How to validate a string field for proper phone number format

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2016 07:17 AM
Hi,
You can create an onChange client script for the same such as:
var phone = g_form.getValue('mobile_phone');
if (!(/^[0-9]{10}$/.test(phone))) {
alert("Enter 10 digit phone number");
g_form.setValue('mobile_phone',' ')
}
If not resolved , let me know.
Thanks and Hope it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2018 01:27 PM
function onChange(control, oldValue, newValue, isLoading)
{
if (isLoading || newValue == '') return;
var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
newValue = newValue.replace(/[^0-9]/g, '');
var digits = newValue.length;
if (newValue.match(phoneno))
{
var code = '';
var area = newValue.substring(0, 3);
var prefix = newValue.substring(3, 6);
var line = newValue.substring(6);
g_form.setValue('u_poc_phone_number', '(' + area + ') ' + prefix + '-' + line);
}
else
{
alert('Please check the phone number and enter it again.');
g_form.setValue('u_poc_phone_number', oldValue);
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2018 03:33 AM
Hey everyone, I don't know if this is still useful for anyone so I will just leave it here.
ServiceNow has a type of field called Phone Number (E164). This type of field validates if a Phone number is valid depending on the Country code.
This type of field exists only on the backend, so if you need this validation on the Service Portal you can use the following script include:
validatePhoneNumber: function(phoneNumber) {
var local = (this.getParameter('sysparm_local') == 'true');
var strict = (this.getParameter('sysparm_strict') == 'true');
var showText = (this.getParameter('sysparm_showText') == 'true');
var gePN = new GlideElementPhoneNumber();
var valid = gePN.setPhoneNumber(phoneNumber, strict);
var matchType = gePN.getPartialMatchType() + "";
var returnValue = null;
if (valid) {
if (local)
returnValue = gePN.getLocalDisplayValue();
else
returnValue = gePN.getGlobalDisplayValue();
var country = gePN.getTerritory();
var optionValue = gePN.getGlobalDialingCode() + ","
+ gePN.getLocalDialingCode() + ","
+ gePN.isLocalFollowsGlobal();
}
var country = gePN.getTerritory();
if (matchType == 'GLOBAL') // Failed to match global part of phone number
country = gs.getMessage("Other / Unknown");
var obj = {};
obj.isValid = valid;
obj.country = escape(country);
return JSON.stringify(obj);
},​
- isValid (Boolean saying if the phone number is valid)
- country (String with the name of the country owner of the provided Country code)
Hope this helps anyone in the future.
Mark it as correct if it solves your issues.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2021 02:14 PM
Hi Diogo,
I would like to try this script include, can you share the client script where you are calling it? Are you just using a String field in the catalog?
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-13-2021 12:48 AM
Hey Brad,
gs.info(validatePhoneNumber('+351961111111'));
function validatePhoneNumber(phoneNumber) {
var gePN = new GlideElementPhoneNumber();
var valid = gePN.setPhoneNumber(phoneNumber);
var matchType = gePN.getPartialMatchType() + "";
if (valid) {
var country = gePN.getTerritory();
}
var country = gePN.getTerritory();
if (matchType == 'GLOBAL') // Failed to match global part of phone number
country = gs.getMessage("Other / Unknown");
var obj = {};
obj.isValid = valid;
obj.country = escape(country);
return JSON.stringify(obj);
}
This sample is a bit cleaner.
This example validates a Portuguese phone number. In this case it's a valid one.
Just create an onChange client script on your phone number field (single line text) and call an ajax function that calls this sample. Pass your phone number as a parameter.
You can run this sample on a Background Script and it should work.
Let me know if you need help!