简体   繁体   English

使用jQuery的表单验证不起作用

[英]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. 由于我是jQuery的新手,因此无法使用jQuery验证来验证我的第一个以及非常简单的登录表单。
I have searched for jQuery validation and have also seen many questions posted on the site but found them helpless for myself. 我搜索了jQuery验证,也看到了许多问题发布在网站上,但发现它们对我自己无能为力。
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. 我认为问题是onLoad()事件后代码未运行。 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" }
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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