Basic regex for a "shared" phone number

I need a regular expression (for use on the ASP.NET website) to check phone numbers. It should be flexible and the only limitations are:

  • must be at least 9 digits
  • no letters
  • may include spaces, hyphens, one (+)

I searched for SO and Regexlib.com, but I get expressions with a lot of restrictions, for example. UK phone or USA etc.

+5
source share
7 answers

It’s better to ask the user to fill out his country, and then apply the regular expression for that country. Each country has its own format for phone numbers.

+4
^\s*\+?\s*([0-9][\s-]*){9,}$

:

^           # Start of the string
  \s*       # Ignore leading whitespace
  \+?       # An optional plus
  \s*       # followed by an optional space or multiple spaces
  (
     [0-9]  # A digit
     [\s-]* # followed by an optional space or dash or more than one of those
  )
   {9,}     # That appears nine or more times
$           # End of the string

, ; , , . RegexOptions.IgnorePatternWhitespace #.

+18

\+?[\d- ]{9,}

, , .

, . ,

\+?[\d]{9,}

+6

^ [0-9- +] + $

<asp:RegularExpressionValidator runat="server" id="rgfvphone" controltovalidate="[control id]" validationexpression="^[0-9-+ ]+$" errormessage="Please enter valid phone!" />
+2
@"^(?:\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"

. .

0

This one is super-generic, allowing you to use only the characters you expect in a given phone number, including #. It ignores any format patterns.

@"^([\(\)\+0-9\s\-\#]+)$"
0
source

All Articles