API to validate Phone Numbers

peter_repan
Tera Guru

Hi all,

 

I am looking for a server-side API to validate Phone Numbers against E.164 format or convert the Phone Number to E.164 format.

 

I know there is a field type "Phone Number (E.164)" doing the check and conversion automatically, but I would need to use the same validation logic in my custom server-side scripts. 

 

So far I found undocumented Script Includes "PhoneNumberValidationUtils, PhoneNumberFormatter" or documented "NotifyUtil" (needs plugin activation) which seem to have some methods to validate phone numbers and format the numbers.

 

I'm curious, does anybody have experience with any other API or methods how to validate & convert Phone numbers in server-side script against E.164 format? 

1 REPLY 1

Lalit Bhatt
ServiceNow Employee
ServiceNow Employee

Try below line of codes: Basically valid variable will tell you if number is valid or not. 

//Malaysia country code 60
//area code 03 or 3 (leading zero can be omitted)
//subscriber number should be 8 digits

// Change below fullPh variable to test validation and display format
//var fullPh = '+60 03 123 4567';// 7 digits after area code 03 - validation fails
//var fullPh = '+60 3 1234 567';// 7 digits after area code 3 - validation fails
//var fullPh = '+60 03 2123 4567';// 8 digits after area code 03 - validates
var fullPh = '+60 3 2123 4567';// 8 digits after area code 3 - validates

var gePN = new GlideElementPhoneNumber();
var valid = gePN.setPhoneNumber(fullPh, true);
var country = gePN.getTerritory();

gs.print("getTerritory() "+gePN.getTerritory());
gs.print("valid "+valid);
gs.print("getValue "+gePN.getValue());
gs.print("isStrict "+gePN.isStrict());
gs.print("getGlobalDisplayValue "+gePN.getGlobalDisplayValue());
gs.print("getLocalDisplayValue "+gePN.getLocalDisplayValue());