1

Its the newbie again.. I am trying to design a form that takes a username and a password and validates them. However first i want to make sure that the user fills in both the fields before i pass it on to the servlet or any other server related component. I wish to do so using javascript which generates an alert button, more like a JOptionPane when either or both the fields are empty. My code is


index.jsp


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    <script src="newjavascript.js"></script>
</head>
<body>
    <h1>Hello World!</h1>
    <form id="form" method="post" action="final.jsp">
    Student name :<br><input type="text" value="" name="username"   id="idusername"><br><br>
    Password :<br><input type="text" value="" name="password" id="idpassword"><br><br>
    <input type="submit" value="Register" />
</form>
</body>
</html>

I pass the above onto a javascript file newjavascript.js which is


$(document).ready(function(){
var form = $("#form");
var username = $("#idusername");
    var password = $("idpassword");
//On Submitting
form.submit(function(){
    if(validateName() & validatePassword())
        return true;
    else
    {
        alert('Some fields are not filled correctly. Please fill them correctly.');
        return false;
    }
});

function validateName(){

    if(username.val().length < 1){
        return false;
    }

    else{
        return true;
    }
}

    function validatePassword(){


    if(password.val().length <5){
        return false;
    }

    else{           
        return true;
    }
}
});

Is my javascript code wrong or i am not properly able to pass on the parameters to it ??

4
  • 1
    Your first line will invalidate the doctype. doctype must be on first line. Not directly answering the problem, but just saying. Commented Jun 20, 2012 at 16:13
  • Okay.. I made the doctype statement the very first line. But it still does not solve the problem !! Commented Jun 20, 2012 at 16:16
  • Okay add # to var password. and add reference to Jquery to validate $(document).ready(function(){}); Commented Jun 20, 2012 at 16:26
  • I have done it before, but yet unable to get it validated !! Commented Jun 20, 2012 at 16:33

3 Answers 3

1
<script>
        $(document).ready(function () {
            var form = $("#form");
            alert(form);
            var username = $("#idusername");
            var password = $("#idpassword");
            //On Submitting
            form.on("submit",function () {
                if (validateName() & validatePassword())
                    return true;
                else {
                    alert('Some fields are not filled correctly. Please fill them correctly.');
                    return false;
                }
            });

            function validateName() {

                if (username.val().length < 1) {
                    return false;
                }

                else {
                    return true;
                }
            }

            function validatePassword() {
                alert(password);

                if (password.val().length < 5) {
                    return false;
                }

                else {
                    return true;
                }
            }
        });


    </script>
Sign up to request clarification or add additional context in comments.

1 Comment

The above code gives some error in the var form = $("#form"); line
1

Do like this

$(document).ready(function(){
var form = $("#form");
form.submit(function(){
    if(validateName() && validatePassword())
        return true;
    else
    {
        alert('Some fields are not filled correctly. Please fill them correctly.');
        return false;
    }
});

});
function validateName(){
var username = $("#idusername");

    if(username.val().length < 1){
        return false;
    }

    else{
        return true;
    }
}

    function validatePassword(){
    var password = $("#idpassword");//previously it was wrong


    if(password.val().length <5){
        return false;
    }

    else{           
        return true;
    }
}

1 Comment

I tried your modified code. But i donot get any validation even when i click submit button without entering any of the fileds..
1

Here's a fiddle to a corrected version of your code on with the following fixes:

  1. you must use $("#idpassword");
  2. You should use && for the "logical and" comparison - not & for a "bitwise and." The code works with the bitwise &, but you're not trying to do a bitwise operation.

ADDITION

  1. You must prevent the default form submission.

    form.submit(function(event) { ... if (bad) { event.preventDefault(); }

(I had this in the fiddle but forgot to mention it)

1 Comment

I did the two corrections you suggested but yet still as sonn as i click Register without filling any field the "final.jsp" page opens u !!

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.