jQuery验证不验证表单,但不会抛出错误

时间:2011-03-11 08:53:35

标签: jquery validation jquery-validate

我正在使用jQuery Validate和jQuery Mobile进行移动应用程序。我一直试图让这项工作多年,看不出有什么问题。基本上,表单提交时不会触发任何验证。

我已经回溯了很多,并从http://jquery.bassistance.de/validate/demo/的示例页面复制了jquery和validate.js的版本。仍然不起作用:(

    <script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function() { alert("submitted!"); }
    });

    $().ready(function() {
        // validate the comment form when it is submitted
        //$("#commentForm").validate();

        // validate signup form on keyup and submit
        $("#signupForm").validate({
            rules: {
                username: {
                    required: true,
                    minlength: 2
                },
                password: {
                    required: true,
                    minlength: 5
                },
                password2: {
                    required: true,
                    minlength: 5,
                    equalTo: "#password"
                },
                email: {
                    required: true,
                    email: true
                },
            },
            messages: {
                username: {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                password2: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long",
                    equalTo: "Please enter the same password as above"
                },
                email: "Please enter a valid email address"
            }
        });
       </script>

表单正在PHP中初始化:

       echo "<form id=\"signupForm\" action='".$_SERVER['PHP_SELF']."' method=\"post\">";

3 个答案:

答案 0 :(得分:2)

你的js代码似乎无效,这是有效的代码

$.validator.setDefaults({
     submitHandler: function() { alert("submitted!"); }
 });

 $().ready(function() {
     // validate the comment form when it is submitted
     //$("#commentForm").validate();

     // validate signup form on keyup and submit
     $("#signupForm").validate({
         rules: {
             username: {
                 required: true,
                 minlength: 2
             },
             password: {
                 required: true,
                 minlength: 5
             },
             password2: {
                 required: true,
                 minlength: 5,
                 equalTo: "#password"
             },
             email: {
                 required: true,
                 email: true
             }
         },
         messages: {
             username: {
                 required: "Please enter a username",
                 minlength: "Your username must consist of at least 2 characters"
             },
             password: {
                 required: "Please provide a password",
                 minlength: "Your password must be at least 5 characters long"
             },
             password2: {
                 required: "Please provide a password",
                 minlength: "Your password must be at least 5 characters long",
                 equalTo: "Please enter the same password as above"
             },
             email: "Please enter a valid email address"
         }
     });});

答案 1 :(得分:0)

至少在IE中可能导致问题的一件事是在规则结构的最终“email”元素之后的尾随逗号。结构中的最后一个元素后面不应该有逗号。

答案 2 :(得分:0)

您已在规则中添加了“逗号” - &gt;电子邮件块(在最后),这就是你的代码无效的原因。

电子邮件:{required:true,                             email:true},

应该是

电子邮件:{required:true,                             电子邮件:true}