Regex for a phone number

maryc
Tera Contributor

Hello,

 

I want a REGEX for a phone number to accept only numbers and only in this format

 

Allows formats of (999) 999-9999, 999-999-9999 ONLY

 

Thanks

 

2 ACCEPTED SOLUTIONS

Vrushali  Kolte
Mega Sage

Hi @maryc ,

you can try  these regex : -   ^\(?\d{3}\)?[-\s]?\d{3}-\d{4}$   or   ^\((\d{3})\)\s*(\d{3})-(\d{4})|(\d{3})-(\d{3})-(\d{4})$

 

If my answer solved your issue, please mark my answer as Correct ✔️ & Helpful 👍 based on the Impact.

View solution in original post

Samaksh Wani
Giga Sage
Giga Sage

Hello @maryc 

 

 

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

 

 

 

Use this in OnChange Client Script 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh

 

View solution in original post

3 REPLIES 3

Vrushali  Kolte
Mega Sage

Hi @maryc ,

you can try  these regex : -   ^\(?\d{3}\)?[-\s]?\d{3}-\d{4}$   or   ^\((\d{3})\)\s*(\d{3})-(\d{4})|(\d{3})-(\d{3})-(\d{4})$

 

If my answer solved your issue, please mark my answer as Correct ✔️ & Helpful 👍 based on the Impact.

Samaksh Wani
Giga Sage
Giga Sage

Hello @maryc 

 

 

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

 

 

 

Use this in OnChange Client Script 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh

 

Samaksh Wani
Giga Sage
Giga Sage

Hello @maryc 

 

As per new community rules, You can mark multiple solutions.

 

Plz mark my solution as Accept, if you find it helpful. it will help future users to get the correct solutions.

 

Regards,

Samaksh