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

The advantage of doing this as a "Question Regular Expression" is that is reusable throughout the catalog without having to introduce a client script on every catalog item.  My catalog has multiple items where there is some sort of phone variable, and there are more to come.  If I have a regex value for phone, I just have to select a drop-down value for the phone variable.

Vaishnavi Lathk
Mega Sage
Mega Sage

Hello ,

For validation you have to create on change client script on mobile number field

Attaching screen shot for the same

find_real_file.png 

******************code snippet********************

function onChange(control, oldValue, newValue, isLoading) {
if(isLoading || newValue == ''){
return;
}
// Allows formats of (999) 999-9999, 999-999-9999, and 9999999999
var pattern = /^[(]?(\d{3})[)]?[-|\s]?(\d{3})[-|\s]?(\d{4})$/;
if(!pattern.test(newValue)){
alert('Phone enter a valid phone number');
g_form.setValue('u_mobile_number', '');
}
}

**************************************************

 Thank you,

PLEASE mark my ANSWER as CORRECT if it served your purpose.

 

Hi Vaishnavi,

Thanks a lot for your response. It really helped. Appreciate it.

Thanks. 

Aman Kumar S
Kilo Patron

Hey,

I think there is a no code way to achieve this:

Follow below article for the same:

Field validation: Regular Expressions

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Hitoshi Ozawa
Giga Sage
Giga Sage
Hi Gulzar
If using regular expression, create a "Question Regular Expressions" ("Service Catalog" > "Catalog Variables" > "Variable Validation Regex"
I don't think regular expression alone will be able to fully validate all e164 numbers because the standard just have maximum number of digits. In actual usage, number of digits is defined by the country code.
 
Test using regex101.com
find_real_file.png
 
find_real_file.png
 
find_real_file.png
 
 
find_real_file.png
 
find_real_file.png