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 6 of 6

Thread: AndyB

  1. #1
    New to the CF scene
    Join Date
    Jun 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    AndyB

    Something (or some things) are wrong with this. The purpose should be clear, but my code doesn't work. Anyone want to point out theobvious mistake(s)??

    <html>
    <head>

    <script type="text/javascript">
    function goToIt(){
    if (document.myform.exper.value>="10") {window.location="http://whatever.com/oldenuf.html"}
    return true;
    }
    else;
    {window.location="http://whatever.com/nogood.html"}
    return true;
    }
    </script>

    </head>
    <body>
    <form name="myform" onSubmit="return goToIt()" method="get">
    How many years experience do you have:&nbsp;&nbsp;<input type="text" name="exper" size="8">
    <input type="submit" name="submit" value="Press to Continue">
    </form>
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    function goToIt(){
    if (document.myform.exper.value>="10")
    you're comparing strings, not numbers
    {window.location="http://whatever.com/oldenuf.html"}
    return true;
    why return anything? you've just changed pages
    }
    else;
    semicolon after else statement
    {window.location="http://whatever.com/nogood.html"}
    return true;
    ditto
    }

    Might as well filter the input:

    <html>
    <head>
    <script type="text/javascript">

    function goToIt(field) {
    if (/^\d+$/.test(field.value)) { //number entered?
    field.form.action = (Number(field.value)>=10) ? //convert entry to number,set form action
    'http://whatever.com/oldenuf.html' : 'http://whatever.com/nogood.html';
    return true; //submit
    }
    alert('Please enter a number.'); //prompt,focus,cancel
    field.focus();
    field.select();
    return false;
    }

    </script>

    </head>
    <body>
    <form name="myform" onsubmit="return goToIt(exper)">
    How many years experience do you have: <input type="text" name="exper" size="8">
    <input type="submit" value="Press to Continue">
    </form>
    </body>
    </html>
    Last edited by adios; 07-11-2002 at 02:43 AM.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, first of all you should write :

    if (document.myform...... >= 10 ) instead of if(docu..... >= "10")

    You shouldn't put that ; after else
    and it's no need to use an "else" clause at all! coz if the condition of if is right, then the return true works and you'll be out of your function, no need to put that else. now I put the correct program that really works here!!


    <html>
    <head>

    <script type="text/javascript">
    function goToIt()
    {
    if (document.myform.exper.value >= 10)
    {
    window.location="http://whatever.com/oldenuf.html"
    return true;
    }
    window.location="http://whatever.com/nogood.html"
    return true;
    }
    </script>

    </head>
    <body>
    <form name="myform" method="get">
    How many years experience do you have:
    <input type="text" name="exper" size="8">
    <input type="button" name="submit" value="Press to Continue" onclick="goToIt();">
    </form>
    </body>
    </html>

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the function should be:

    <script type="text/javascript">
    function goToIt(){
    if (document.myform.exper.value>="10") {window.location="http://whatever.com/oldenuf.html";
    return true;
    }
    else
    {window.location="http://whatever.com/nogood.html" ;
    return true; }
    }
    </script>


    i don't know about the return true and false parts, i didn't bother to try to get the function in the first place. i just know this:

    the if-else syntax is
    if(condition){
    whatever statement...;
    }--> you shouldn't have put 'return true;' after the curlies-both times
    else{
    whatever...;
    }

    you closed the curly brackets too soon in both cases, and the return trues were cruelly excluded from the curly brackets of the 'if' and 'else' clauses they so truly deserved to be in

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    FYI there is nothing wrong when you compare a number against a string, as long as the string is nothing but digits:

    10 > '5' // true
    10 > '5px' // false

    But it only will typecast one operand:

    '10' > '5' // false

    because both operands are compared as strings.

    This was causing a problem here:

    document.myform.exper.value >= "10"

    Both are strings. If you did either:

    Number(document.myform.exper.value) >= "10"

    or

    document.myform.exper.value >= 10

    it would have been fine. You should explicitly typecast though just to be clear and safe:

    Number(document.myform.exper.value) >= 10

    There was also that semicolon problem with the else statement someone else pointed out.


    You could shorten your entire code with the use of the ternary operator as well:

    window.location.href = (parseInt(document.myform.exper.value) >= 10) ? 'http://whatever.com/oldenuf.html' : 'http://whatever.com/nogood.html';

    And throw the return true after that if you must, or you could just get plain silly and utilize awful coding habits for fun:

    return Boolean(window.location.href = (parseInt(document.myform.exper.value) >= 10) ? 'http://whatever.com/oldenuf.html' : 'http://whatever.com/nogood.html');

    Not recommended though .

  • #6
    New to the CF scene
    Join Date
    Jun 2002
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks all. Assistance appreciated.


  •  

    Posting Permissions

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