Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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}$/;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading