1.页面引入css、js
<link href="https://www.jb51.net/vendor/bootstrap/css/bootstrap.css"/> <link href="https://www.jb51.net/dist/css/bootstrapValidator.css"/> <script type="text/javascript" src="https://www.jb51.net/vendor/jquery/jquery-1.10.2.min.js"></script> <script type="text/javascript" src="https://www.jb51.net/vendor/bootstrap/js/bootstrap.min.js"></script> <script type="text/javascript" src="https://www.jb51.net/dist/js/bootstrapValidator.js"></script>
2.页面表单
<div tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div> <div> <div> <button type="button" data-dismiss="modal" aria-hidden="true">× </button> <h4>系统用户信息</h4> </div> <div> <form role="form" action="" method="post"> <input type="hidden"> <div> <label for="userName">用户名</label> <div> <input type="text"> </div> </div> <div> <label for="userPassword">密码</label> <div> <input type="password"> </div> </div> <div> <label for="email">Email</label> <div> <input type="text"> </div> </div> <div> <label for="alias">别名</label> <div> <input type="text"> </div> </div> <div> <label for="personId">隶属人员</label> <div> <input type="text"> </div> <label for="enabled">是否可用</label> <div> <input type="radio" value="true" checked>是 <input type="radio" value="false">否 </div> </div> <div> <label for="accountExpired">账号是否过期</label> <div> <input type="radio" value="true">是 <input type="radio" value="false" checked>否 </div> <label for="accountLocked">账号是否锁定</label> <div> <input type="radio" value="true">是 <input type="radio" value="false" checked>否 </div> </div> <div> <label for="credentialsExpired">密码是否过期</label> <div> <input type="radio" value="true">是 <input type="radio" value="false" checked>否 </div> <label for="superAdmin">是否超级管理员</label> <div> <input type="radio" value="true">是 <input type="radio" value="false" checked>否 </div> </div> <div> <label for="memo">备注</label> <div> <textarea cols="80" rows="3"></textarea> </div> </div> <div> <button type="button" >保存</button> <button type="button" >重置</button> <button type="button" >取消</button> </div> </form> </div> </div><!-- /.modal-content -->
3.js初始化验证
//验证表单 var validatorForm = { //验证规则 validatorReuls:function(){ $("#detailForm").bootstrapValidator({ feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { userName:{ validators: { notEmpty: { message: '用户名不能为空' }, remote: {////ajax验证。服务器端返回的 result:{"valid",true or false} 向服务发送当前input name值,获得一个json数据。例表示正确:{"valid",true} url: contextPath+"/sysUser/username", type:"GET", data: function(validator) { var x_={ userName: validator.getFieldElements('userName').val() }; return x_; }, message: '用户名已注册,请重新输入' } } }, email: { validators: { notEmpty: { message: '邮箱不能为空' }, regexp: {//正则表达式 regexp: Regex.email, message: '邮箱地址格式不正确' } } } ,userPassword: { validators: { notEmpty: { message: '密码不能为空' }, regexp: { regexp: Regex.password_6_18, message: '密码只能输入6-18个字母、数字、下划线 ' } } } } }); }, //验证表单 validate: function(formId){ $('#'+formId).data('bootstrapValidator').validate(); }, //验证表单是否通过验证 isValid : function(formId){ return $('#'+formId).data('bootstrapValidator').isValid() }, //清空表单验证 clearValidate : function(formId){ $('#'+formId).bootstrapValidator('resetForm'); } }
4.最后呈现的效果
bootstrapValidator官网:bootstapValidator