RegEx challenge (or just seems that way)

Nope. In my world (and in my contact list), any phone number that doesn’t start with + is an invalid number :stuck_out_tongue:

Validating phone numbers with regex is a bit of a slippery slope. Every time you think you’ve nailed it, you’ll find another edge case that breaks it. And before you know it you’ll have something that looks like this:

/^
 \+        [[:space:]]* [0-9] [0-9.[:space:]-]*
 ( \( [0-9.[:space:]-]* [0-9] [0-9.[:space:]-]* \) )?
 (    [0-9.[:space:]-]* [0-9] [0-9.[:space:]-]*    )?
 ( [[:space:]]+ 
   ext . 
      [0-9.[:space:]-]* [0-9] [0-9.[:space:]-]* 
 )?
$/xi

If you really want to do it, then you’re probably better off using an existing library.

1 Like