Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Location
    Oregon
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help w/script -- a validating function

    Hi all,

    As you can see I'm a newbie. I've been in a basic CIS class for a few weeks now. We've been learning Javascript slowly but surely, but I still think it's a little fast for me. I've been able to complete my projects by using my books and notes, but this week I'm having troubles debugging. I can't discern the problem's with Firefox's javascript console...

    With this function (and yes, I have it called correctly from my HTML, so don't worry about that ) I need to validate a phone number from a form. I need to ensure that the phone number has a valid area code of either 541 or 503. Also, it needs to be 10 digits long. After that is all verified, I need an alert that lets the user know that the number is valid.

    Who can pinpoint my error(s)?

    PHP Code:
    function validPhone (f){
    var 
    phone=f.phone.value
    var prefix=phone.substr(0,3)
    var 
    long=phone.length
    var numeros=parseInt(phone)

    if ((
    prefix != "541")||(prefix!="503")){
        
    alert("That is not a valid Oregon area code. Please re-enter");
        return 
    false;
    }

    if (
    long!=10){
        
    alert("That isn't long enough");
        return 
    false;
    }

    if (
    isNaN (numeros)){
        
    alert("Please enter only numbers");
        return 
    false;
    }

    alert("That is a valid phone number. Thanks");
    return 
    true

    Thanks for any insight!

    Kristina
    Last edited by schmazz; 11-12-2004 at 08:45 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Well, you parse the ints out of the string then check that they're numbers. They can't be anything but numbers after using parseInt.

    Other than that I don't see anything wrong. What errors are you getting?

  • #3
    New to the CF scene
    Join Date
    Nov 2004
    Location
    Oregon
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, it doesn't run at all

    For errors, I'm getting
    1. Missing variable name
    var long=phone.length

    2. validPhone is not defined

    Thanks,
    Kristina

    Aha. Long is a reserved JS word... let me change that and see if it works.
    Ok, it seems kind of ok. But then...
    When I put in a correct phone number, I am still getting the my alert "That is not a valid Oregon area code". What is wrong with my logic?
    Last edited by schmazz; 11-12-2004 at 09:22 PM.

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    168
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Your first condition should use logical 'and' like so :

    if((prefix != "541") && (prefix != "503"))

    Just as a variation, you might want to compare your approach with a solution using regular expressions. Both approaches are equally valid.

    Regular expression approach :

    function validate()
    {
    // a variable to hold the reference to the phone number field
    t = document.forms[0].telnumber;
    //first check length of phone number
    if(t.value.length != 10)
    {
    alert("The phone number is not the correct length.");
    t.focus();
    return false;
    }
    //now check if the number starts correctly
    if(t.value.match(/(^541)|(^503)/)==null)
    {
    alert("The phone number should start with 541 or 503.");
    t.focus();
    return false;
    }
    //check if all ten characters are digits
    if(t.value.match(/\d{10}/)==null)
    {
    alert("The phone number should contain ten digits.");
    t.focus();
    return false;
    }
    alert("The phone number," + t.value + " ,is valid.");
    return true;
    }
    </script>

  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Generally, when you ask for help, you need to say what you want the script to do, and what it actually does, plus any errors you receive and what line in your code the errors correspond to(e.g. a comment on the line that says: "THIS IS WHERE THE %$##$ ERROR I'VE SPENT 4 HOURS TRYING TO FIX IS". Making the font color red is a good choice for a comment like that, plus adding a redfaced, angry smilie, , can be cathartic too.)

    One problem with your function is your choice of the 'logical operator' ||. The code within an if-statement is executed when the if-statement conditional is true. So, let's look at this conditional:

    Code:
    if ((prefix != "541")||(prefix!="503")){
        alert("That is not a valid Oregon area code. Please re-enter");
        return false;
    }

    Let's say the user enters "541" as the area code, which makes it a valid area code. This:

    prefix != "541"

    evaluates as false because, in fact, the area code does equal "541", and this:

    prefix!="503"

    evalutes as true because the area code does not equal "503". So, your if conditional becomes this:

    Code:
    if (false || true){
        alert("That is not a valid Oregon area code. Please re-enter");
        return false;
    }
    For an || conditional to evaluate as true, only one term has to be true. That means the alert() in the if statement will execute. So, even though a user enters a valid area code, they will get a message saying the area code is invalid.

  • #6
    New to the CF scene
    Join Date
    Nov 2004
    Location
    Oregon
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Puffin the Erb, you nailed it. I am really, really new to JS so I try to stick to altering examples my teacher has given me, so the long latter explanation didn't help to much. But you corrected my operator, and now it makes sense (and runs, too). I'm not very mathmatically inclined so it's taking me a while to get this kind of coding down (anything slightly math-related scares me, frankly).

    And 7stud, thanks for the tips on how to better ask for help. Puffin barely beat you to the solution, though

    And hemebond, thanks for starting!

    Thanks,
    Kristina

  • #7
    New Coder
    Join Date
    Nov 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    Aha. Long is a reserved JS word
    What are you using for a text editor? You can buy editors for $30 online(e.g. EditPlus) and there are some that are free(e.g. crimsoneditor), which have syntax highlighting, so it is readily apparent when you are using a keyword, have missed a closing paranthesis or bracket, etc. Mine has an autocomplete feature that I have set up so when I type:

    dgi

    it automatically writes:

    document.getElementById(" ");

    which is a huge time saver.
    Last edited by 7stud; 11-12-2004 at 09:39 PM.

  • #8
    New Coder
    Join Date
    Nov 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I try to stick to altering examples my teacher has given me, so the long latter explanation didn't help to much.
    ttt


  • #9
    New to the CF scene
    Join Date
    Nov 2004
    Location
    Oregon
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by 7stud
    ttt

    What?

    ttt? I am not familiar with your crazy shorthand jargon.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •