The CreatorCon Call for Content is officially open! Get started here.

How to validate valid phone numbers in the service catalog

santrym
Mega Expert

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?

1 ACCEPTED SOLUTION
9 REPLIES 9

Inactive_Us1474
Giga Guru

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.


verdakosnett
Tera Expert

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;
}
}

Diogo Figueira
Tera Contributor

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);
},​
 
This is using the OOTB functionality to validate Phone Number (E164) type fields.
It will return an object with two values:
  • 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.

 

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

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!