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

Thread: New exercise

  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    136
    Thanks
    4
    Thanked 0 Times in 0 Posts

    New exercise

    The following is what I have to do:

    Try this code out:

    <script language="javascript" type="text/javascript">
    function numtester1() {
    x=document.getElementById("tb2").value;
    if(isNaN(x)) {
    alert("you did not enter a number");
    } else {
    alert("you did enter a number");
    }
    }
    </script>
    <form name="form2">
    <input type="text" id="tb2">
    <input type="button" value="test input" onclick="numtester1();" />
    </form>


    You enter a value inside the text box. When you click on the button the function numtester1 is called. The variable x gets assigned the value of what is inside the text box. The if statement then tests if x is not a number. If x is not a number or isNAN(x) returns true then the alert box has "you did not enter a number" in it. If x is a number or isNAN(x) returns false then the alert box has "you did enter a number" in it.

    This is what I did:

    <script type="text/javascript">

    function numtester1() {
    x=document.getElementById("tb2").value;
    if(isNaN(x)) {
    alert("you did not enter a number");
    } else {
    alert("you did enter a number");
    }
    }



    </script>
    <form name="form2">
    <input type="text" value="5" />
    <input type="button"onclick="numtester1();" />
    </form>

    What am I missing?

    Thanks in advance.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Here is a hint: Review your HTML and tell me how this value:

    Code:
    x=document.getElementById("tb2").value;
    can possibly be found with what you have...
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    136
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I just figured it out now:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <script type="text/javascript">

    function numtester1() {
    x=document.getElementById("tb2").value;
    if(isNaN(x)) {
    alert("you did not enter a number");
    } else {
    alert("you did enter a number");
    }
    }



    </script>
    <form name="form2">
    <input type="" id="tb2" />
    <input type="button"onclick="numtester1();" />
    </form>
    </body>
    </html>




    Thanks


  •  

    Posting Permissions

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