Phone number regex with optional characters

I’ve attempted multiple times to make it but unfortunately I ran out of ideas. I need to make regex for phone number validation. I need to have regex in which user can input his phone number with country code but only first character must be optional. For example I need:

+1XXX 123-45-67-89
+11XX 123-45-67-89
+111X 123-45-67-89
+1111 123-45-67-89

Regex has to work with whitespace (after country code) and with special character "-" and X is an optional character which user should be able to skip.
Looking forward for your help.

>Solution :

May be regex below will help , it is a bit explicit , but should meet your requirements :

let regex=/^\+\d{1,4}\s*\d{3}\s*\-\s*\d{2}\s*\-\s*\d{2}\s*\-\s*\d{2}$/;

Leave a Reply