jQuery:验证规则不按预期工作

时间:2014-05-26 19:27:11

标签: javascript jquery validation jquery-plugins

<script type="text/javascript">
    $(document).ready(function () {
        $("#loginForm").validate({
            rules: {
                email: {
                    required: true,
                    minlength: 10
                },
                passwd: {
                    required: true,
                    minlength: 8
                }
            },
            messages: {
                email: {
                    required: "Please enter your email",
                    minlength: "Minlength has to be 10"
                },
                passwd: {
                    required: "Please enter your email",
                    minlength: "Minlength has to be 8"
                }
            },
            submitHandler: function (form) {
                alert('valid form submit');
            }
        });
    })
</script>

我在HTML头_

中有这个
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js"></script>

在关闭body标签之前结束jQuery。

当我使用http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js时,以下代码在jsfiddle上运行得非常好 http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js

但是在我的浏览器上,总是提交处理程序。 http://jsfiddle.net/4937t/

我非常感谢你的时间。

1 个答案:

答案 0 :(得分:1)

我不确定您是如何在html文档中包含这些脚本的,但是让我向您展示一个工作示例。它适用于我的chrome。我猜你的脚本的位置是错误的。希望这有效

<!doctype html>
<html>
<body>
   <form id="loginForm">
     <input name="email" type="text"/><br/>
     <input name="passwd" type="password"/><br/>
     <input type="submit" />
   </form>
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
   <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js"></script>
   <script>
    $(document).ready(function () {
        $("#loginForm").validate({
            rules: {
                email: {
                    required: true,
                    minlength: 10
                },
                passwd: {
                    required: true,
                    minlength: 8
                }
            },
            messages: {
                email: {
                    required: "Please enter your email",
                    minlength: "Minlength has to be 10"
                },
                passwd: {
                    required: "Please enter your password",
                    minlength: "Minlength has to be 8"
                }
            },

            submitHandler: function (form) {
                alert('valid form submit');
            }
        });
    });
  </script>
  </body>
  </html>