0

As i'm very new to jQuery i'm having trouble validating my very first as well as very simple login form using jQuery validation.
I have searched for jQuery validation and have also seen many questions posted on the site but found them helpless for myself.
Here is my code.
Please help me finding out my mistake.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>apply.html</title>
<script type="text/javascript" src="jquery-1.11.1.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
</head>
<body>
<script type="text/javascript">
    $("#testForm").validate({
        rules: {
            uname: { required: true },
            pwd: { required: true }
        },
        messages: {
            uname: { required: "Username can not be left blank" },
            pwd: { required: "Please enter Password" }
        }
    });
</script>
<form action="#" method="POST" id="testForm">
    Username :
    <input type="text" name="uname" /><br>
    Password :
    <input type="password" name="pwd" /><br>
    <input type="submit" value="Push">
</form>
</body>
</html>

EDIT : the problem i'm facing is that on submitting the form no error message message is showing i.e. no validation taking place.

2
  • Keep in mind that any client-side validation can be bypassed so you have to validate once again on the server-side. Commented Jun 28, 2015 at 15:27
  • @DanGetz : It simply submitting the form without any error message. Sorry for not mentioning that. Commented Jun 28, 2015 at 15:28

1 Answer 1

1

I've reproduced your code with the fiddle and it works properly. I assume the problem is that the code is not run after onLoad() event. Here's my solution:

$(function(){
    $("#testForm").validate({
        rules: {
            uname: { required: true },
            pwd: { required: true }
        },
        messages: {
            uname: { required: "Username can not be left blank" },
            pwd: { required: "Please enter Password" }
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help :) i just added $(document).ready(function(){ // my jQuery code }); and it worked

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.