Reputation: 13
I wanted to create a phone number validation but with some specific requirements .not accepting more than 10 max numbers and not accepting more than 20 characters if it has special characters (numbers|space|.|+|(|)|-|/|X/).whats the best approach to dot this with regex
i tried this but /^(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/ but not getting the expecting result
Upvotes: 0
Views: 67
Reputation: 163207
One way could be asserting that there are not 21 characters present in the string using a negative lookahead ^(?!.{21}$)
Then match 10 times a single digit with optional characters that you consider special on the left and right using [() .+|\/-]*
^(?!.{21}$)[() .+|\/-]*(?:\d[() .+|\/-]*){10}[() .+|\/-]*$
See a regex demo.
Upvotes: 1