- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2022 10:33 PM
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
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-26-2022 10:59 PM
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');
}
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2022 01:58 AM
Hi Hitoshi,
Thank you very much for your response. I will use this resource to verify my Regex going forward.
Best Regards,
Gulzar Manuja