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.

Regex for phone numbers.

Andrew122
Mega Expert

I'm not too good with Regex.  Does anyone know how I could format it so that a phone number field will accept only numbers, parentheses, and minus signs?  Currently it's set up as /^\d+$/ and the field only accepts numbers.

1 ACCEPTED SOLUTION

Dinesh Nikam
Mega Guru

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.

View solution in original post

5 REPLIES 5

Dinesh Nikam
Mega Guru

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.