0

I am trying to validate the textbox in the login Xhtml and once I figured out the code, the section class'container' in the code is not working.

How can I write the code and validate the textbox properly?

<head>
    <script type = 'text/javascript'>
        function validate() {
            if (document.getElementById("login").value == "") {
                alert("User name may not be blank");
            } else if (document.getElementById("password").value == "") {
                alert("Password may not be blank.");
            }
        }
    </script> 
    <title>Login Form</title>
    <link rel="stylesheet" href="css/style.css"/> 
</head>    
<body>
    <h1> Login</h1>
    <section class="container">
        <div>         
            <form method="post" action="index.xhtml" onsubmit="return validate();">
                <p>
                    <input type="text" name="login" value="" id="login" placeholder="Username or Email" />
                    <input type="password" name="password" value="" id="password" placeholder="Password" />
                </p>
                <p>
                    <label>
                        <input type="checkbox" name="remember_me" id="remember_me" />Remember me on this computer
                    </label>
                </p>
                <p class="submit">
                    <input type="submit" name="commit" value="Login" onclick="validate();" />
                    <button type="reset" value="Clear">Clear</button>
                </p>
            </form>
            <form action= "http://localhost:8080/ChattBank/LoginServlet" method="post"/>
        </div>
    </section>
</body>

3 Answers 3

2

You can use jquery validation. It's very simple.

<form method="post" action="index.xhtml" id="loginForm">
...
</form>
$("#loginForm").validate({
rules: {
  "login": {
    required: true
  },
  "password": {
    required: true
  },
  messages: {
    "login": "This field is required",
    "password": "This field is required",
  }
}
});
Sign up to request clarification or add additional context in comments.

Comments

1

EDIT

Try this: (Replace your forms with this)

<input type="text" name="login" value="" id="login" placeholder="Username or Email"  />
<input type="password" name="password" value="" id="password" placeholder="Password" />
</p>
<p>
    <label>
        <input type="checkbox" name="remember_me" id="remember_me" />Remember me on this computer</label>
</p>
<p class="submit">
    <input type="submit" name="commit" value="Login" onclick="validate();" />
    <button type="reset" value="Clear">Clear</button>
</p>
<script>
    function validate() {
        if (document.getElementById("login").value == "") {
            alert("User name may not be blank");
        } else if (document.getElementById("password").value == "") {
            alert("Password may not be blank.");
        }
    }
</script>

Here is a working fiddle

5 Comments

I implemented it but it's not working still, what did I do wrong?
@SpencerWieczorek What other problems does the fiddle have? It is working fine for me.
@JohnroePauloCañamaque It is now that you updated it, the only thing left you need to add is the form submission when the values are valid.
@SpencerWieczorek If I do that, there will be no more way for the OP to explore on his work;
@JohnroePauloCañamaque The OP originally had a form going to a different page.
0

We are trying to use document.getElementById("login") and document.getElementById("password"), which are both null. Because your two input fields do not have id as an attribute. You will need to add that:

<input type="text" ... id="login"  />
...
<input type="password" ... id="password"  />

Also validate() takes no attributes, so don't pass this on the login input.

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.