Reputation: 6569
I'm trying to validate a phone number in the following format:
1-123-456-7890
123-456-7890
1234567890
I wish all three of these instances to be acceptable, however I do not know how to make it so that all dashes or no dashes are required. Here is what I have so far:
^(1-)?\d{3}-\d{3}-\d{4}$
^[0-9]{10,11}$
Is there a way to tell it to validate 1 regex or the other in a single regex expression?
Upvotes: 1
Views: 71
Reputation: 74
What about just counting the number of digits? You don't seem to be filtering by area code, prefix, etc.
In JS, that would be:
number.ok = number.replace( /[^0-9]/g ,'').length >= 10
For 10 or more digits. There are NO valid numbers in the U.S., which start with 1, so you can strip all leading 1's before the count, and then check for a length of 10.
Upvotes: 0
Reputation: 67968
^(?:(?:(1-)?\d{3}-\d{3}-\d{4})|[0-9]{10,11})$
You can try this.See demo.
https://regex101.com/r/cK4iV0/13
Upvotes: 2