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:
I searched for SO and Regexlib.com, but I get expressions with a lot of restrictions, for example. UK phone or USA etc.
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.
^\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 #.
RegexOptions.IgnorePatternWhitespace
\+?[\d- ]{9,}
, , .
, . ,
\+?[\d]{9,}
^ [0-9- +] + $
<asp:RegularExpressionValidator runat="server" id="rgfvphone" controltovalidate="[control id]" validationexpression="^[0-9-+ ]+$" errormessage="Please enter valid phone!" />
http://regexlib.com/DisplayPatterns.aspx?cattabindex=6&categoryId=7
@"^(?:\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"
. .
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\-\#]+)$"