简体   繁体   中英

Form Validation using jQuery not working

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 ie no validation taking place.

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" }
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM