- The first Regular Expression Library on the Web!

Please support RegExLib Sponsors


Regular Expression Details

Title Test Find Pattern Title
^(?:(?<1>[(])?(?<AreaCode>[2-9]\d{2})(?(1)[)])(?(1)(?<2>[ ])|(?:(?<3>[-])|(?<4>[ ])))?)?(?<Prefix>[1-9]\d{2})(?(AreaCode)(?:(?(1)(?(2)[- ]|[-]?))|(?(3)[-])|(?(4)[- ]))|[- ]?)(?<Suffix>\d{4})(?:[ ]?[xX]?(?<Ext>\d{2,4}))?$
Regular expression for validating US telephone numbers with OPTIONAL area code, and OPTIONAL extension. Matches various permutations of formatting characters (parenthesis, space, dash). Parses the telephone number area code, prefix, suffix, and extension to named groups to facilitate program manipulation. Area code is optional and can optionally be enclosed in parentheses. Rejects area codes that begin with 0 or 1 and prefixes that begin with 0. Extension is optional and can be optionally preceded by a space and/or &quot;x&quot; or &quot;X&quot;, and matches on 2 to 4 digits. Rejects all telephone numbers that do not match on exactly 7 digits, or on exactly 10 digits with the optional area code, not counting the extension or the formatting characters.
333-4444 | (222) 333-4444 | 222-333-4444 X55
222333 4444 | (222)-333 4444 | 333-4444-5555
Author Rating: The rating for this expression. Jerry Schmersahl
Your Rating
Bad Good

Enter New Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: fails test
Name: developer
Date: 12/25/2004 5:48:06 PM
doesn't work for examples

Title: Got Error in IE 6
Name: Kyle
Date: 7/6/2004 5:48:34 PM
Got Error in IE 6

Title: IE6
Name: JJ
Date: 6/5/2004 5:16:05 PM
Doesn't work with IE6

Copyright © 2001-2024, | ASP.NET Tutorials