To create Regex for a phone number field

Gulzar Manuja2
Kilo Guru

Dear All,

Greetings !!!

I have a requirement to create a Phone Number variable on a catalog item which is supposed to follow E164 format. Since E164 type is not available on a catalog item, I have created a Single Line Text type field, however, I am facing issues while creating Regex for this field. My requirement is to allow users to enter numbers, spaces and special characters such as brackets, hyphen, plus sign. On top of that, they should not be able to enter alphabets in this field. A few valid examples of valid phone numbers are:


1. +1 (201) 515-1633
2. +919876543210
3. 9876512345
4. 201-515-1633
5. 987 654 321

I would like to keep spaces and special characters(brackets, plus sign, hyphen, spaces) to be optional. I have tried creating Regex for this requirement but apparently I haven't able to create the correct one. Could someone help me to create Regex for this requirement please?

Thanks & Regards,

Gulzar Manuja 

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

Can try this should work

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

/*^\s*                #Line start, match any whitespaces at the beginning if any.
(?:\+?(\d{1,3}))?   #GROUP 1: The country code. Optional.
[-. (]*             #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(\d{3})             #GROUP 2: The Area Code. Required.
[-. )]*             #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(\d{3})             #GROUP 3: The Exchange number. Required.
[-. ]*              #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(\d{4})             #Group 4: The Subscriber Number. Required.
(?: *x(\d+))?       #Group 5: The Extension number. Optional.
\s*$                #Match any ending whitespaces if any and the end of string.*/

    var number= g_form.getValue('phonenumber');

var pattern = /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/;
if(!pattern.test(number)){
alert('Phone enter a valid phone number');
}
}

Regards
Harish

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you will find numerous links for getting help on Phone number RegEx; did you try those?

Regular expression to match standard 10 digit phone number

Regular Expression Validation For Indian Phone Number and Mobile number

Regard
Ankur

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

Hi Ankur,

Thanks for your response. I did check a few articles but I wasn't able to build a Regex to fulfill my requirement. Thanks for sharing the links. I believe they will be helpful for me to troubleshoot the issues with my Regex.

Thanks. 

Harish KM
Kilo Patron
Kilo Patron

Can try this should work

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

/*^\s*                #Line start, match any whitespaces at the beginning if any.
(?:\+?(\d{1,3}))?   #GROUP 1: The country code. Optional.
[-. (]*             #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(\d{3})             #GROUP 2: The Area Code. Required.
[-. )]*             #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(\d{3})             #GROUP 3: The Exchange number. Required.
[-. ]*              #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(\d{4})             #Group 4: The Subscriber Number. Required.
(?: *x(\d+))?       #Group 5: The Extension number. Optional.
\s*$                #Match any ending whitespaces if any and the end of string.*/

    var number= g_form.getValue('phonenumber');

var pattern = /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/;
if(!pattern.test(number)){
alert('Phone enter a valid phone number');
}
}

Regards
Harish

Hi Harish,

Thank you very much for explaining the entire Regex by breaking it down into chunks. It made it very easier to understand. Marking it as correct. 

Thanks.