Reg exp for phone number

Discussion in 'Javascript' started by Erik Jälevik, Nov 6, 2003.

  1. 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
     
    Erik Jälevik, Nov 6, 2003
    #1
    1. Advertising

  2. Erik Jälevik

    Evertjan. Guest

    Erik Jälevik wrote on 06 nov 2003 in comp.lang.javascript:

    > 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.


    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)
     
    Evertjan., Nov 6, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Rowland

    Reg exp: matching relative path only.

    Andrew Rowland, Aug 2, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,151
    Andrew Rowland
    Aug 2, 2003
  2. psk

    Newbie-Reg Exp

    psk, Jan 16, 2004, in forum: Perl
    Replies:
    2
    Views:
    1,372
    Gunnar Hjalmarsson
    Jan 19, 2004
  3. PerlE

    Reg Exp Help

    PerlE, Jan 30, 2004, in forum: Perl
    Replies:
    0
    Views:
    510
    PerlE
    Jan 30, 2004
  4. Ken Chesak

    reg exp

    Ken Chesak, Aug 26, 2004, in forum: Perl
    Replies:
    0
    Views:
    633
    Ken Chesak
    Aug 26, 2004
  5. Ken Chesak

    reg exp

    Ken Chesak, Aug 30, 2004, in forum: Perl
    Replies:
    5
    Views:
    655
Loading...

Share This Page