regex in javascript

Discussion in 'Web Design and Development' started by cwesty, Apr 20, 2007.

  1. cwesty macrumors member

    Joined:
    Oct 22, 2005
    #1
    hi all,

    i'm ''trying'' to validate a phone number using a regular expression in js.

    The code is below, but i have a problem. The code works to the extent that it knows when the telephone number is wrong, but when i type the correct format ie - 01222000111 it just clears the field.

    Any ideas???

    Here's the javascript:
    re = /^(\s*\(?0\d{5}\)?\s*\d{6}\s*)|(\s*\(?0\d{3}\)?\s*\d{3}\s*\d{4}\s*)/

    function validateForm(form) {
    validPhone = re.exec(form.phone.value)
    if (validPhone) {
    form.phone.value = ""


    }
    else {
    alert(form.phone.value + " isn't a valid number")
    form.phone.focus()

    }

    return false;

    }

    and the html:
    <form method="post" action="" onsubmit="return validateForm(this)">
    <input name="phone" type="text" size="50" />
    <input type="reset" /> <input type="submit" value="submit" />

    </form>

    many thanks
    c
     
  2. andy89 macrumors 6502

    Joined:
    May 22, 2005
    Location:
    Folkestone, England
    #2
    Im not sure, but I think you need to use:

    /^(\s*\(?0\d{5}\)?\s*\d{6}\s*)|(\s*\(?0\d{3}\)?\s*\d{3}\s*\d{4}\s*)/.match

    instead... maybe.
     
  3. cwesty thread starter macrumors member

    Joined:
    Oct 22, 2005
    #3
    unfortunately adding the .match didn't work...

    i think it must be part of the function, but not sure why it won't submit...
     
  4. epochblue macrumors 68000

    epochblue

    Joined:
    Aug 12, 2005
    Location:
    Nashville, TN
    #4
    I'd take out the \s* stuff at the beginning and the end and just add a .trim() function to the JavaScript string class to eat all the whitespace at the beginning and end of the line. That'll save you at least a little readability weirdness.

    Beyond that, it looks like the first half of the regex is looking for a 12-digit number that starts with 0 (0 + \d{5} + \d{6} == 12 digits), but the test number you provided is only 11.

    I think you should use .test instead of .exec as well.

    I'd also add $ at the end of the regex because currently "011111111111adfadsfasdfasdfasdf" would pass your regex.

    I'd also break up the reg-ex into multiple, easier-to-read expressions and test each one separately [ex: if( regex1.test(str) && regex2.test(str) ) { //do something } ). That'll help the readability (and maintainability) of the regex tremendously.
     
  5. epochblue macrumors 68000

    epochblue

    Joined:
    Aug 12, 2005
    Location:
    Nashville, TN
    #5
    Oh, and it won't submit because you're always returning false.

    After it clears the form field (form.phone.value = ""), it drops out of the if/else control block and returns false which cancels the event.

    Try returning true after the field clearing (although, if you haven't submitted the form yet, clearing the field probably isn't the best idea).
     
  6. cwesty thread starter macrumors member

    Joined:
    Oct 22, 2005
    #6
    thanks - i'll have a look into that,,,,


    much appreciated!
     
  7. cwesty thread starter macrumors member

    Joined:
    Oct 22, 2005
    #7
    sorry - where should the return true bit sit in the code?

    and should it have { and } included??
     
  8. cwesty thread starter macrumors member

    Joined:
    Oct 22, 2005
    #8
    update..

    Hi All!

    OK, i finally got the regular expression to work, but, when added to the main .js file i have ( which has 2 small functions to test the first name and surnames have been filledin ) it now seems to ignore them and concentrate on the phone number..
    ANy ideas how i ensure that all my code (functions, reg ex's) all work??


    info appreciated!

    Cheers
    c
     

Share This Page