#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Javascript Beginner - What's wrong with this code?


    I'm new at Javascript and I didn't able to figure out what's wrong with my code. Would you please help me about this?


    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <script language="javascript">
    function control(){
    if(document.forms(0).txtusername.value=="" || document.forms(0).txtpassword.value==""){
    alert("can't be blank");
    return false;
    }
    }
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <form action="login" method="POST" onSubmit="return control()">
    username <input type ="text" name="txtusername"/>
    password <input type="password" name="txtpassword"/>
    <input type="submit" value="login"/>
    </form>
    </body>
    </html>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0
    It sends me login page even both text fields are blank.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    What's "forms(0)"? I guess you meant to write "forms[0]". Then it should work.

IMN logo majestic logo threadwatch logo seochat tools logo