Search code examples
javascriptregexvalidation

Regex to exclude spaces from the phone number


I've been trying to make a regex for the phone number validation.

It should take a string of minimum 10 digits with "+" symbol.

Here it is: ^(?=.*[0-9]).{10,}$

However, it still lets through spaces in the start/middle/end.

Is it possible to make something like +415555556 go through, but not something like +415555556 or +415555556

Thank a lot in advance!


Solution

  • Your regex lets through more than just spaces.

    Here's a regex that fits the criteria "Starts with a + followed by 10 or more digits and nothing else"

    /^\+[0-9]{10,}$/