Need validation for phone number field

Mounika M
Tera Contributor

Hi All,

We have a requirement, that we need to validate phone number with country code. Could you please help on this.

Thanks in Advance!!

3 REPLIES 3

Tom Sienkiewicz
Mega Sage

Hi,

you can use the Phone number field - it does that out of the box:

https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/field-administratio...

If there is a reason you con't do that then you can just grab any of the RegEx floating around:

https://regexr.com/38pvb

You would still need to add any custom prefix checking logic.

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

phone number format varies based on country.

is this for for field or variable?

what format you require? you can simply use RegEx to validate the phone number

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Snehangshu Sark
Mega Guru

Hi @Mounika M,

 

The format of area code and phone number is different between countries. Would require an explicit format of area code and phone number. So, I'd suggest you store all possible area codes in an array and use it to validate all the phone numbers. See the script below. You can use it, it's tested. Or, you can use a regex as well.

var phoneNumber = "+818884869885";
var areaCodes = ["+91","91","1","+1"];  // add explicit area code/country code to this array
var isValid = false;
areaCodes.forEach(function(x){ if(phoneNumber.startsWith(x)) return isValid =true;});
gs.info("isValid: "+isValid);

 

Regards,

Snehangshu Sarkar

 

Please mark my answer as correct if it resolves your query.