Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Regular expression which allows numbers, spaces, plus sign, hyphen and brackets

Noor Mohammad
Giga Contributor

I am checking with regex to validate Numbers, space, plus sign, hypen, brackets but nothing worked out.

 

Tried with

1. /^(?=.*[0-9])[- +()0-9]+$/

2.  /^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/

Examples:

+987-123444

8792372372366

+(971) - 7362653425

+98765342

7364 3473 76437

87 83 8344 9834

2 REPLIES 2

Community Alums
Not applicable

Hi @Noor Mohammad ,

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');
}
}

 

Noor Mohammad
Giga Contributor

I am creating a choice option in regex type where directly in the variable we can select the regex validators so the pattern you mentioned was not able to work I already tried it and added in my question as well.