Monday 22 April 2019

Regex Phone number with dashes




I'm trying to get a regex that will validate the following formats:



234-567-8901
1-234-567-8901


I have tried various examples, but not sure how to get the one that will take into account the possible 1- at the begging and still inforce the 234-567-8901.



Rules are, any digit, 2 or 3 dashes, if 3 dashes then 11 numberes, if 2 dashes then 10 numbers.




^[0-9-]*$
^[\d-]+$
^[\d-]+$

Answer



You can use:



^(1-)?\d{3}-\d{3}-\d{4}$

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...