user749798
user749798

Reputation: 5370

Regex to prevent certain phone area codes

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

Answers (1)

kerwei
kerwei

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

Related Questions