Reputation: 5370
I'm trying to prevent users from typing a phone number that starts with area code 555.
Below is my phone regex. How can I ensure the first 3 are not 555?
I thought about stopping numbers 5 for each, but it would have to be 555 consecutive. Can it be done in one regex or do I need 2?
pattern="^(+0?1\s)?(?\d{3})?[\s.-]?\d{3}[\s.-]?\d{4}$"
Upvotes: 1
Views: 241
Reputation: 1842
Have you tried using negative lookahead?
pattern="^(?!555)[\d\s-]+"
This looks for strings that contain digits, whitespaces and dashes, with the condition that it doesn't start with 555.
Test it out here: https://regex101.com/r/aVrZEl/1
Upvotes: 2