jquery-validate校验

开源地址:https://github.com/jquery-validation/jquery-validation

校验select添加如下属性:

ignore: ":hidden:not(select)",//解决无法校验select 

 

校验js

     $('#frm').validate({
                focusInvalid: false,
                onfocusout: function (element) {
                    var obj = $(element), type = element.type;
                    if (type === 'text' || type === 'textarea') {
                        obj.val($.trim(obj.val())).valid();
                    } else {
                        obj.valid();
                    }
                },
                rules: {
                    Password: {
                        required: true
                    },
                    newPassword: {
                        required: true,
                        same: true
                    },
                    PasswordConfirm: {
                        required: true,
                        equalTo: '#newPassword'
                    }
                },
                messages: {
                    Password: {
                        required: '请填写旧密码'
                    },
                    newPassword: {
                        required: '请填写新密码',
                        same: '旧密码不能同新密码相同'
                    },
                    PasswordConfirm: {
                        required: '请填写确认密码',
                        equalTo: '两次密码要相同'
                    }
                },
                submitHandler: function (form) {
                    var data = $(form).formSerialize();
                    delete data['PasswordConfirm'];
                    data['Password'] = md5(data['Password']);
                    data['Token'] = common.getParameter('token');
                }
            });

 

自定义校验属性 用法相同

 jQuery.validator.addMethod("same", function (value, element) {
            return this.optional(element) || same(value);
        }, "新密码不能与老密码重复");


        function same(pwd) {
            var oldPwd = $("#password").val();
            if (oldPwd == pwd)
                return false;
            else
                return true;
        }  

 

posted @ 2017-08-14 10:46  ice.ko  阅读(395)  评论(0编辑  收藏  举报