0

I am relatively new to web development and I'm trying to do some form validation with javascript but my javascript block is not executing and I really don't know why. Any ideas?

<html>
<head>
    <title>Login</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link type = "text/css" rel = "stylesheet" href = "formato.css" />
    <script type = "text/javascript">
        function validate(){
            var x = document.forms[LoginInformation][username].value;
            var y = document.forms[LoginInformation][password].value;
            if(document.LoginInformation){
                alert("Enter a username");
                return false;
            }
            if(y===null || x ===""){
                alert("Enter a password");
                return false;
            }
        }
    </script>
</head>
<body>
    <center>
        <div id = "login">

        <form method = "post" name = "LoginInformation" onsubmit = "return (validate());" action="./TestLogin">
            <fieldset>
                <legend>Informaci&oacute;n de Login</legend>
                <label>Usuario:<br />
                <input type="text" name="username" size ="15"/></label><br /> 
                <label>Contrase&ntilde;a:<br />
                <input type="password" name="password" size ="15"/></label><br />
                <label><br /><input type = "submit" name = "login" value = "Login"/></label><br />
            </fieldset>
        </form>
        </div>
    </center>
</body>

2
  • 1
    the very first thing to try when debugging JavaScript is to have your browser's developer console open. Hit F12 in Chrome or Firefox to pop it up, then reload your page and perform whatever actions needed to trigger your JavaScript, and see what happens. Commented May 1, 2014 at 18:25
  • @orzechowskid Sorry, but what am I supposed to see? (or not to see) Commented May 1, 2014 at 18:36

3 Answers 3

1

Please pass the form id and control id as string as mentioned below

var x = document.forms["LoginInformation"]["username"].value; var y = document.forms["LoginInformation"]["password"].value;

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

   <script type = "text/javascript">
    function validate(){
        var x = document.getElementbyID("username");
        var y = document.getElementbyID("password");
        if(x==""){
            alert("Enter a username");
            return false;
        }
        if(y==null || x ==""){
            alert("Enter a password");
            return false;
        }
     }
   </script>

and then all you code in between

<form method="post" name="LoginInformation" action="./TestLogin">
        <fieldset>
            <legend>Informaci&oacute;n de Login</legend>
            <label>Usuario:<br />
            <input type="text" id="username" size ="15"/></label><br /> 
            <label>Contrase&ntilde;a:<br />
            <input type="password" id="password" size ="15"/></label><br />
            <label><br /><input type ="submit" name ="login" value = "Login" onClick="validate()"/></label><br />
        </fieldset>
    </form>

5 Comments

Wow, thanks it worked. What is the difference between onClick and onsubmit?
@Darwin57721 By the way what is action="./TestLogin in your form?
oh, that is my servlet to test that the login information is being sent correctly to the database
ONClick will work for that specific control event, on submit is more complicated.Take a look at w3schools.com it may help.
@Darwin57721 It is a common courtesy here to accept the answer when it was what you needed
0

in your validate function you need to have LoginInformation, username, and password enclosed in quotation marks. If it's not, your JavaScript interpreter will consider them as regular ol' JavaScript variables, which they're not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.