Help with javascript

Discussion in 'Computer Science & Culture' started by Brutus1964, Mar 31, 2008.

Thread Status:
Not open for further replies.
  1. Brutus1964 We are not alone! Registered Senior Member

    Messages:
    608
    I need help with a function to validate a phone number that allows for
    perens () and dashes -. Here is what I have so far...

    function checkNumeric(num, errorMsg){
    var numeric = /^[0-9]+$/;
    if(num.value.match(numeric)){
    return true;
    }else{
    alert(errorMsg);
    num.focus();
    return false;
    }
    }
    the line var numeric = /^[0-9]+$/;
    is where I need help. This will only allow digits 1 - 9. How can
    I make it also allow ()- input in the textbox?
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Brutus1964 We are not alone! Registered Senior Member

    Messages:
    608
    I think I figured it out.

    var numeric = /^[0-9,(,),--]+$/;

    I don't know if that is the proper syntax but it works.
    I tried some other symbols just to make sure.

    If anyone else has any better ideas let me know.

    Please Register or Log in to view the hidden image!

     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. RubiksMaster Real eyes realize real lies Registered Senior Member

    Messages:
    1,646
    I'm quite rusty with regular expressions, so take this with a grain of salt.

    First of all I know you have to escape the parentheses, because they are reserved and they have a meaning of their own. Like this

    /^[0-9,\(,\),--]+$/

    That's assuming the rest of it is correct, which I'm not sure it is.

    I don't know what the commas in there do. That's the part that seems invalid to me. Also the "--".

    Also this only does one character. You need a "*" after the square brackets.
    /^[0-9,\(,\),--]*$/
    meaning you can see that digit/paren/dash multiple times. I see that you have the plus sign, but I think that does the same character that many times, rather than that expression multiple times. That means you could have 99999, but not 98765.

    There have to be more criteria though. For example, it wouldn't be valid to have the phone number "8)6-5--57(2(" which would be matched by that expression. Think about it in deeper detail. For example, if you had a parentheses, it would only be around the area code at the beginning. Thus you would only ever expect the "(" as the very first character, right?
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Brutus1964 We are not alone! Registered Senior Member

    Messages:
    608
    Here are the instructions for the assignment.

    I don't think I need to go beyond just allowing for dashes and parens. I don't think the Prof expects us to match a phone number format exactly.

    I did change the line of code to this:

    var numeric = /^[0-9,\(,\),\-,\" "]*$/;

    It does work in this format. The \- is allowing slashes.
    It wasn't allowing for any space between (555) 212-1111
    between the area code and the phone number so I added \" ",
    and that worked to allow a space.

    For the alpha numeric part of the assignment for Name input
    I changed it to:

    var alpha = /^[0-9a-zA-Z-,\" "]*$/;

    This is allowing digits from 1-9, and upper and lower case, plus a space
    between first and last name.

    It seems to work. Thanks for the help. Let me know if you have any
    other ideas.

    Please Register or Log in to view the hidden image!

     
  8. Stryder Keeper of "good" ideas. Valued Senior Member

    Messages:
    13,105
    That's fine for the straight forwards US based numbers, it's when you start applying it to "international" phone numbers. (I'm sure this isn't necessary for your particular project, but you'd might like to know anyway)

    A UK phone number might be placed on the net as being:

    +44 (0)1234 123456

    Notice it adds a plus sign "+" to the input field. The above number (which I hope doesn't actually work) is meant for people phoning from "other countries" into the UK, the "+" usually means that you first have to dial the International code from your country before the other numbers. In the case of the (0) it's actually dropped from being dialed when phoning from abroad, however if you were calling within the UK the phone number would be:

    (01234) 123456

    (Notice the Zero is then used in the prefix/area code)

    For the most part this International addition probably won't be necessary if the website itself is identified as being "US only". Usually large websites might make different versions for different countries, which means you'd only have to concentrate on your locality.
     
  9. RubiksMaster Real eyes realize real lies Registered Senior Member

    Messages:
    1,646
    Actually I just realized it can be a lot simpler than what I posted before.

    Start with the area code:

    (\(\d{3}\))?

    This says that you will have an open-paren, 3 digits, and a close-paren. This whole expression occurs 0 or more times (the "?"), assuming you want the area code optional.

    Then go on to the regular number, which will have 3 digits, an optional "-" and 4 more digits.
    \d{3}-?d{4}

    put it all together:

    (\(\d{3}\))?\s\d{3}-?d{4}

    The "\s" means a space.

    You can get more sophisticated by allowing either a space or a hyphen or nothing:

    (\(\d{3}\))?(\s|-)?\d{3}(\s|-)?d{4}

    As far as I can tell, this would match the following formats:
    (555)123-4567
    (555)1234567
    (555)-123-4567
    123-4567
    ...

    If you wanted the area code to have optional parentheses like 555-123-4567, that might be a little harder, because you would have to have it remember whether the first parenthesis occured. I think there is a way to do this, or else you can just have an either-or like this:

    ((\(\d{3}\))|\d{3})?

    I think that is the better option. In that case, the whole statement would be:

    var phoneNumber = /^((\(\d{3}\))|\d{3})?(\s|-)?\d{3}(\s|-)?d{4}$/;
    if(num.value.match(phoneNumber)){


    It's long, but it works way better than what it was before.
     
Thread Status:
Not open for further replies.

Share This Page