//验证 function verifica(form) { this.form = form; this.fields = null; //验证(页面初始化的时候执行) this.verification = function () { //if (!this.fields) throw new Error("验证规则(fields)不能为空"); $(this.form).bootstrapValidator({ message: 'This value is not valid', feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: this.fields }); } //是否验证通过(提交时执行) this.isValid = function () { var data = $(this.form).data('bootstrapValidator'); if (data) { data.validate(); if (!data.isValid()) { return false; } } return true; } //清除验证效果 this.clearVeri = function () { $(this.form).bootstrapValidator('resetForm', true);//消除验证效果 } }
//验证 var verif = new verifica("#addModal .form-horizontal"); verif.fields = { newPassword: { message: '验证失败', validators: { notEmpty: { message: '不能为空' }, stringLength: { min: 6, max: 20, message: "密码长度在6到20之间" } } }, inputNewPassword: { message: '密码重复还没有验证', validators: { notEmpty: { message: '密码重复不能为空' }, stringLength: { min: 6, max: 20, message: '密码长度在6到20之间' }, identical: { field: 'newPassword', message: '两次密码不同请重新输入' } } }, code: { message: '编号验证失败', validators: { notEmpty: { message: '编号不能为空' }, regexp: { regexp: /^[a-zA-Z0-9]+$/, message: '编号只能包含大写、小写、数字' } } }, email: { validators: { notEmpty: { message: '邮箱是必填项' }, emailAddress: { message: '邮箱格式不正确' } } } };
regexp: { regexp: /^[^\u4E00-\u9FA5A-Za-z]+/, message: '只包含数字、符号' } regexp: { regexp: /^[^\u4e00-\u9fa5]+$/, message: '编号不包含中文' }, regexp: { regexp: /^[\u4E00-\u9FA5A-Za-z]+$/, message: '只包含中英文' }