regular expressions and search() - vertical bar problem

Discussion in 'Javascript' started by Ward Cleaver, Sep 28, 2004.

  1. Ward Cleaver

    Ward Cleaver Guest

    Hi. I have an assignment to do some validating of a form using
    javascript and mostly the search() method. I'm having problems
    getting a positive validation for phone numbers like "123-456-7890"
    and "123.456.7890" but not like "123.456-7890". The regular
    expression I'm using now is something like this:
    ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3}.\d{3}.\d{4}$)/);
    Which to me looks like it SHOULD do what I want it to and not come
    back with an ok=0 for a phone number using both hyphens and periods.
    Please help.
     
    Ward Cleaver, Sep 28, 2004
    #1
    1. Advertising

  2. Ward Cleaver

    RobG Guest

    Ward Cleaver wrote:

    > ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3}.\d{3}.\d{4}$)/);


    I'm guessing...

    (/(^\d{3}(-|.)\d{3}(-|.)\d{4}$)/)

    should do the trick for all cases.

    You could also use:

    (/((\d{3}(-|.)){2}\d{4}$)/)

    but that will produce +ve values if extra characters are
    added to the front of the test string - only zero (0) will
    be a correct result or any non-zero value a fail. For the
    sake of saving a few characters, I don't think it's worth
    it.

    Cheers.
     
    RobG, Sep 29, 2004
    #2
    1. Advertising

  3. On 28 Sep 2004 15:45:51 -0700, Ward Cleaver <> wrote:

    > Hi. I have an assignment


    If you mean an educational course assignment, you're probably required to
    mention that you received help.

    > to do some validating of a form using javascript and mostly the search()
    > method.


    String.prototype.search is generally unsuitable for validation. If you
    want to assert that a string matches a pattern, use RegExp.prototype.test:

    /<pattern>/.test(<string>)

    which returns true for a match. The search method returns the position
    where a match was found.

    > I'm having problems getting a positive validation for phone numbers like
    > "123-456-7890" and "123.456.7890" but not like "123.456-7890". The
    > regular expression I'm using now is something like this:
    > ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3}.\d{3}.\d{4}$)/);


    An unescaped dot (period) matches *any* character (except line
    terminators). A literal dot needs to written with a backslash prefix.

    > Which to me looks like it SHOULD do what I want it to and not come back
    > with an ok=0 for a phone number using both hyphens and periods.
    > Please help.


    Though I recommended the test method, I'd probably use
    RegExp.prototype.exec so I could check that the separators match:

    var r = /^\d{3}([.-])\d{3}([.-])\d{4}$/.exec(pn);

    /* If the string didn't match, r will be null.
    * If it did match, r will be an array with element
    * 0 containing the match
    * 1 containing the first remembered match (marked with
    * parentheses)
    * 2 containing the second remembered match
    */
    if(r && (r[1] == r[2])) {
    // Pattern matched and the separators are equal.
    }

    Good luck,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Sep 29, 2004
    #3
  4. JRS: In article <>,
    dated Tue, 28 Sep 2004 15:45:51, seen in news:comp.lang.javascript, Ward
    Cleaver <> posted :
    > I have an assignment to do some validating of a form using
    >javascript and mostly the search() method. I'm having problems
    >getting a positive validation for phone numbers like "123-456-7890"
    >and "123.456.7890" but not like "123.456-7890". The regular
    >expression I'm using now is something like this:
    >ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3}.\d{3}.\d{4}$)/);
    >Which to me looks like it SHOULD do what I want it to and not come
    >back with an ok=0 for a phone number using both hyphens and periods.



    Dot matches any character; use \. . Testing only with more-or-less
    valid data is a distressingly common mistake; a test with a non-allowed
    first separator would have given a clue. Build up such expressions in
    small stages, testing as you go.

    Search returns a number; to get a Boolean for OK, use test.

    OK = /^(\d\d\d)([-\.])(\d\d\d)\2(\d\d\d\d)$/.test(pn)

    Be aware that by using that format only you exclude many of the
    telephones in North America and elsewhere.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Sep 29, 2004
    #4
  5. Ward Cleaver

    rh Guest

    "Michael Winter" wrote:
    <...>
    >
    > Though I recommended the test method, I'd probably use
    > RegExp.prototype.exec so I could check that the separators match:
    >
    > var r = /^\d{3}([.-])\d{3}([.-])\d{4}$/.exec(pn);
    >
    > /* If the string didn't match, r will be null.
    > * If it did match, r will be an array with element
    > * 0 containing the match
    > * 1 containing the first remembered match (marked with
    > * parentheses)
    > * 2 containing the second remembered match
    > */
    > if(r && (r[1] == r[2])) {
    > // Pattern matched and the separators are equal.
    > }
    >


    Alternatively, including a backreference in the regular expression
    would allow use of the preferred test method, e.g.:

    if ( /^\d{3}([-.])\d{3}\1\d{4}$/.test(pn) ) {
    // Pattern matched and the separators are equal.
    }

    ../rh
     
    rh, Sep 29, 2004
    #5
  6. On 29 Sep 2004 09:20:10 -0700, rh <> wrote:

    [snip]

    > Alternatively, including a backreference in the regular expression would
    > allow use of the preferred test method, e.g.:


    I didn't know they existed as an ECMA-262 conformant pattern but I have
    just found it (section 15.10.2.11). How well supported are they?

    Thanks,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
     
    Michael Winter, Sep 29, 2004
    #6
  7. Ward Cleaver

    rh Guest

    "Michael Winter" <> wrote in message news:<opse3j52gcx13kvk@atlantis>...
    > On 29 Sep 2004 09:20:10 -0700, rh <> wrote:
    >
    > [snip]
    >
    > > Alternatively, including a backreference in the regular expression would
    > > allow use of the preferred test method, e.g.:

    >
    > I didn't know they existed as an ECMA-262 conformant pattern but I have
    > just found it (section 15.10.2.11). How well supported are they?
    >


    RegExp backreferences were present in v1.2. I believe it's possible to
    use said same with gay abandon :).

    ../rh
     
    rh, Sep 30, 2004
    #7
    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. Matt
    Replies:
    0
    Views:
    3,575
  2. John Hann
    Replies:
    0
    Views:
    6,135
    John Hann
    Aug 21, 2004
  3. Matt
    Replies:
    9
    Views:
    71,132
  4. Matt
    Replies:
    9
    Views:
    593
    kaeli
    Aug 23, 2004
  5. Noman Shapiro
    Replies:
    0
    Views:
    245
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page