Reg exp for phone number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erik Jälevik

    #1

    Reg exp for phone number

    I have a regular expression which I believe is correct but it's not behaving
    as expected. It is as follows:

    /\+?\d+[-\s\d]*\d+/

    The idea is that a phone number is allowed to be of the form: optional +
    followed by one or more digits followed by any combination of hyphens,
    spaces and digits followed by at least one digit.

    However, this regexp is letting through all sorts of expressions as long as
    they've got two adjacent digits it seems.

    Grateful for help!

    Thanks,
    Erik


  • Evertjan.

    #2
    Re: Reg exp for phone number

    Erik Jälevik wrote on 06 nov 2003 in comp.lang.javas cript:
    [color=blue]
    > I have a regular expression which I believe is correct but it's not
    > behaving as expected. It is as follows:
    >
    > /\+?\d+[-\s\d]*\d+/
    >
    > The idea is that a phone number is allowed to be of the form: optional
    > + followed by one or more digits followed by any combination of
    > hyphens, spaces and digits followed by at least one digit.
    >
    > However, this regexp is letting through all sorts of expressions as
    > long as they've got two adjacent digits it seems.[/color]

    The expression should have begin and end markers:

    /^\+?\d+[-\s\d]*\d+$/

    even simpler:

    /^\+?\d[-\s\d]*\d$/

    mind the \s also matches other white space types.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    Working...