What's Wrong With This JS Code?

Discussion in 'JavaScript' started by saadi123, Apr 23, 2010.

  1. #1
    On clicking the button, a message is supposed to be displayed in an alert box if all or any one text field is left empty. But nothing happens. Please debug this code.


    <html>
    <head>
    <title>More Than One Form</Title>

    <script>
    <body>
    function checkInput()
    {
    var isok = true;
    var count = document.forms[0].elements.length;
    var i = 0;
    for (i=0; i < count; i++)
    {
    if (documents.forms[0].elements.value == "")
    isOk = false;
    }
    if (isok == false)
    alert("Please Fill In All Responses");

    }

    </script>

    </head>

    <body>

    <h1>Check It Out</h1>

    <form>

    Your Full Name: <input type="text" name="yname"><br><br>
    Email Address: <input type="text" name="email"><br><br>
    Postal Address: <input type="text" name="address"><br>

    </form>
    <hr><br>

    The second form begins here
    <hr>

    <form>
    <input type="button" name="doIt" value="check values" onClick="checkInput()">
    </form>
    </body>

    </html>
     
    saadi123, Apr 23, 2010 IP
  2. drknght

    drknght Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    A few issues:
    1) You have an extra <body> tag inside the <script> tag.
    2) You have a typo on this line: if (documents.forms[0].elements.value == "") ... it should be "document.forms[0]..." ("document" is singular)
    3) You have a typo on this line: if (isok == false) ... it should be "if (isOk == false)" ... you are declaring a new variable, rather than updating the isOk variable. Remember that JS is case sensitive.

    Hope this helps!

    Regds
    drknght

    Check out the corrected code:
     
    drknght, Apr 23, 2010 IP
  3. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Thanks.
    That really worked.

    By the way what silly mistakes that were by me!!!!
    That's really too dumb of me.... :-(
     
    saadi123, Apr 23, 2010 IP
  4. drknght

    drknght Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    My pleasure ... and no worries, I've made my share of mistakes - that's the best way to learn :)
     
    drknght, Apr 24, 2010 IP