Created Regex Phone number expression for US +1 XXX-XXX-XXXX

KB30
Tera Expert

Hello I'm working on creating a regular expression for a US phone number. The format needs to be +1 XXX-XXX-XXXX.

 

I found this post below which provided a Regex expression of ^\(?([0-9]{3})\)?[-]([0-9]{3})[-]([0-9]{4})$ but I'm having issues wit the leading 1.

https://www.servicenow.com/community/developer-forum/help-on-regex-for-phone-number-format-xxx-xxx-x...

 

Does anyone have any tips or assistance on how to complete this? This isn't my strong suit and my first time working on Regex expression so all help would be great!!

2 ACCEPTED SOLUTIONS

@DanielCordick I tried but I can only submit +1. I can not submit a number

View solution in original post

^\+1\s\d{3}[-]\d{3}[-]\d{4}$

 

or 

 

^\+1\s?\(?([0-9]{3})\)?[-]([0-9]{3})[-]([0-9]{4})$

View solution in original post

15 REPLIES 15

Try below

^(\+1)?([0-9]{3})\)?[-]?([0-9]{3})[-]?([0-9]{4})$

this does not allow a space between the 1 and area code

@KB30  try below

^(\+1)?(\s+)?([0-9]{3})\)?[-]?([0-9]{3})[-]?([0-9]{4})$

@Jaspal Singh I can submit with the space between the 1 and the area code but the rest of the format does not work at all for the number.  I can submit numbers with no hyphens

Amit Gujarathi
Giga Sage
Giga Sage

HI @KB30 ,
I trust you are doing great.

Here's a regular expression that matches the phone number format +1 XXX-XXX-XXXX:

^\+1\s?([0-9]{3})[-]([0-9]{3})[-]([0-9]{4})$

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi