jquery.validate ajax方式验证

在做网站的时候有一块需要用到jquery.validate插件 ajax方式的方式来验证原始密码是否正确,研究了研究加上博客园朋友的帮助,终于实现了。贴出代码

<script type="text/javascript">
    $(function () {
        $("#form1").validate({
            rules: {
                pwd: {
                    required: true,
                    remote: {
                        url: "@Url.Action("ValidatePwd", "UserInfo")",
                        type: "post",
                        dataType: "json",
                        data: {
                            pwd: function () {
                                return $("#pwd").val();    //这个是取要验证的密码
                            }
                        },
                        dataFilter: function (data) {    //判断控制器返回的内容
                            if (data == "true") {
                                return true;
                            }
                            else {
                                return false;
                            }
                        }
                    }
                },
                password: {
                    required: true,
                    rangelength: [6, 16]
                },
                confirm_password: {
                    required: true,
                    rangelength: [6, 16],
                    equalTo: "#password"
                }
            },
            messages: {
                pwd: {
                    required: "请填写原始密码!",
                    remote: "原始密码不正确,请重新填写!"    //这个地方如果不写的话,是自带的提示内容,加上就是这个内容。
                },
                password: {
                    required: "请填写新密码",
                    minlength: jQuery.format("登录名长度在6-12位之间!")
                },
                confirm_password: {
                    required: "请填写确认密码!",
                    minlength: "密码需由6-16个字符(数字、字母、下划线)组成!",
                    equalTo: "两次输入密码不一致!"
                }
            },
            onfocus: true,    
            onkeyup: false,    //这个地方要注意,修改去控制器验证的事件。
            onsubmit: false
        });

posted @ 2014-08-16 09:49  lhyterry  阅读(15217)  评论(0编辑  收藏  举报