我正在尝试使用jquery为密码提供客户端验证,但表单在未经验证的情况下提交。我也是jquery

时间:2012-02-29 05:54:51

标签: jquery-plugins

我正在尝试使用jquery为密码提供客户端验证,但表单在未经验证的情况下提交。我也是jquery的新手。

我在这里尝试的是什么 1)检查密码字段,应该不为空 2)比较两个密码。
但表单已提交,我找不到错误。

请帮忙

<!DOCTYPE HTML>
<html>
        <head>
        <title>Form Example</title>
        <script type="text/javascript" src="jquery-1.7.1.js"></script>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
        <script type="text/javascript" src="jquery.form.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#myform").validate({
                    //onkeyup: false;
                    debug: true;
                    rules: {
                        password: {
                            required: true,
                            minlength: 6,
                            maxlength: 12,
                            password: true
                        },
                        password1: {
                            required: true,
                            equalTo: "#password"
                        }
                    },

                    messages: {
                        password: {
                            required: "req",
                            minlength: "min",
                            maxlength: "max"
                         }
                        password1: {
                            required: "req re",
                            equalTo: "not equal"
                        }
                    }
                });
            });
        </script>
        <style>
            label.error { width: 250px; display: inline; color: red;}
        </style>
    </head>
    <body>
        <form name="myform" id="myform" action="" method="POST">
            <label for="password">Choose a password</label>
            <input type="text" name="password" id="password" value="" />
            <label for="password" generated="true" class="error"></label>

            <br/>

            <label for="password1">Retype password</label>
            <input type="text" name="password1" id="password1" value="" />
            <label for="password1" generated="true" class="error"></label>

            <br/>
            <input type="submit" value="join" />
            <input type="submit" value="cancel" class="cancel" />
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

对您的验证功能进行以下更改,它将正常运行

1)删除行debug: true;
2)您在消息部分中错过了逗号。在第一条消息后添加逗号,如下所示......

messages: {
    password: {
        required: "req",
        minlength: "min",
        maxlength: "max"
    },
    password1: {
        required: "req re",
        equalTo: "not equal"
    }
}