Need validation for phone number field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2022 11:17 PM
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!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2022 11:32 PM
Hi,
you can use the Phone number field - it does that out of the box:
If there is a reason you con't do that then you can just grab any of the RegEx floating around:
You would still need to add any custom prefix checking logic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2022 11:36 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2022 11:51 PM
Hi
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.