Regular Expressions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Smith

    #1

    Regular Expressions

    Hello all:

    I was wondering if someone had a good regular expression
    for a phone number. I came up w/on that does validation to
    the point of having an optional area code, but I would like
    to restrict the length of the entire number entered. For
    example, if they enter an eight digit number it would not
    match but a seven digit number would. The following is
    what I have been trying but it does not work:

    [0-9]{3}[-| ]?[0-9]{4}

    Which says 3 digits then either a dash, space or nothing,
    followed by four digits.

    Any ideas?

    Thanks,

    John
  • Brian Davis

    #2
    Regular Expressions


    Here is one I came up with:

    ^(((\((?<par>)) ?)\d{3}(?(par)\ )|)[ \-])?\d{3}[ \-]?\d{4}$

    It matches a 3 digit area code, optionally enclosed in
    parentheses, followed by a space, dash, or nothing, then
    three digits, a space, dash or nothing, then four digits.

    Here are some example matches:

    (123) 456 7890
    (123)4567890
    123-456-7890
    1234567890
    456-7890
    (as well as some fishy ones like)
    123456-7890
    (123)-456 7890


    Here are some example NON-matches

    (123 456-7890 - No matching close parenthesis
    1 1234567890 - Leading 1 (may be important to allow)?
    123 456 7890 - Double spacing


    You probably want to run this through a lot more checks
    to make sure it matches what you want, and, more
    importantly, doesn't match what you don't want.

    If you want to check out other expressions,
    http://www.regexlib.com has some useful ones.


    Brian Davis



    [color=blue]
    >-----Original Message-----
    >Hello all:
    >
    >I was wondering if someone had a good regular expression
    >for a phone number. I came up w/on that does validation[/color]
    to[color=blue]
    >the point of having an optional area code, but I would[/color]
    like[color=blue]
    >to restrict the length of the entire number entered. For
    >example, if they enter an eight digit number it would not
    >match but a seven digit number would. The following is
    >what I have been trying but it does not work:
    >
    >[0-9]{3}[-| ]?[0-9]{4}
    >
    >Which says 3 digits then either a dash, space or nothing,
    >followed by four digits.
    >
    >Any ideas?
    >
    >Thanks,
    >
    >John
    >.
    >[/color]

    Comment

    • John Smith

      #3
      Re: Regular Expressions

      Hey, thanks for the info, I too had a similar one that included checking
      for an area code, but I will give your a try. The main thing I was
      after was restricting length as well (i.e. if they entered 8 digits it
      should not match) but mine still does, so I'll see what yours does.

      Thanks again,

      John Smith

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      Working...