Home > OS >  phone number validation with specific requirements
phone number validation with specific requirements

Time:09-10

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

CodePudding user response:

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.

  • Related