var el = { $jsFrom: $('.js-form'), }; // 检测用户名是否存在 jQuery.validator.addMethod("isexist", function(value, element) { var url=contentPath+"/sysUser/testSysUserExist"; var param={}; var result = false; param.userName = value; param.type=3; $.ajax({ url: url,//查询方法的url data: param, // params type: "post", dataType: "json", async: false, success: function (data) { if(data.data.exist){ //该用户已存在 result = true; } }, error: function () { tools.openST({ title: '注意', text: '验证用户名失败,请稍后刷新后重试', type: 'fail' }); } }); return result; }, '用户不存在,请重新填写.'); var validator = { validator: null, valid: function () { return this.validator.form(); }, reset: function () { this.validator.resetForm(); }, init: function () { var varlidator = el.$jsFrom.validate({ ignore: [],//加上这句话之后,这个验证方法就可以验证表单的隐藏域了,而且不用修改插件代码 rules: { 'account': { required: true, isexist:true }, 'code': { required: true, digits: true, rangelength: [6] }, 'password': { required: true, minlength: 6, maxlength: 20 }, 'confirmPassword': { required: true, minlength: 6, maxlength: 20, equalTo : '#password' } }, messages: { 'account': { required: '请输入您的账号' }, 'code': { required: '请填写验证码', digits: '验证码为纯数字', rangelength: '验证码为 6 位数字' }, 'password': { required: '请输入密码', minlength: "不能小于6个字符", maxlength: "不能大于20个字符" }, 'confirmPassword': { required: '请再次输入密码', minlength: "不能小于6个字符", maxlength: "不能大于20个字符", equalTo: '您输入的密码不一致' } }, errorClass: 'text-warning', errorPlacement: function (error, element) { element.closest('div').append(error); } }); this.validator = varlidator; } }; validator.init();
if (!validator.validator.element( "#account" )) { tools.openST({ title: tools.message.validFail.title, text: '请先填写账户名', type: tools.message.validFail.type }); return; }
if (!validator.valid()) { tools.openST({ title: tools.message.validFail.title, text: tools.message.validFail.text, type: tools.message.validFail.type }); return; }