小案例搞懂—bootstrapvalidator前端表单校验
2020-05-18 22:55
211 查看
Bootstrap Validator入门案例
- Bootstrap Validator数据校验就是表单验证。友好的错误提示能增加用户体验。
- jQuery form validator for Bootstrap,从描述中我们就可以知道它至少需要jQuery、bootstrap的支持
1、导入相关组件
<%--引入bootstrap.css--%> <link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <%--引入BootstrapValidator.css--%> <link href="${pageContext.request.contextPath}/bootstrapvalidator/css/bootstrapValidator.min.css" rel="stylesheet"> <%--引入jquery.js--%> <script src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script> <%--引入Bootstrap.js--%> <script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.min.js"></script> <%--引入BootstrapValidator.js--%> <script src="${pageContext.request.contextPath}/bootstrapvalidator/js/bootstrapValidator.min.js"></script>
2、如果想对某一个字段添加验证规则,需要div(class=“form-group”)包裹,input标签必须有name值,此值为验证匹配的字段。其实就是要符合bootstrap表单结构。
<div align="center"> <form id="myForm" action="${pageContext.request.contextPath}/demo01" method="post"> <div class="form-group"> 账户:<input type="text" name="username"><br> </div> <div class="form-group"> 密码:<input type="text" name="password"><br> </div> <div class="form-group"> <button type="submit">提交</button> </div> </form> </div>
3、使用js添加验证
<script> $(function () {//页面加载完成 $("#myForm").bootstrapValidator({ message : "this is not a valid field",//设置提示信息 fields:{//设置要校验的字段集合 username:{ validators:{ notEmpty:{ message:"账户不能为空" }, stringLength:{ min:6, max:10, message: "账户长度为6-10" }, regexp:{ regexp: /^[a-z0-9]{6,10}$/, message:"账户格式不正确" } } }, password:{ validators:{ notEmpty:{ message:"密码不能为空" }, stringLength:{ min:6, max:10, message:"密码长度为6-10" }, regexp:{ regexp: /^[a-z0-9]{6,10}$/, message:"密码格式不正确" }, different:{ message:"密码不能和手机号一致", field:"user_tel" } } } } }); }) </script>
4、运行效果如下:
相关文章推荐
- 学习LayUI时自研的表单参数校验框架案例分析
- JQuery案例之表单校验
- VeeValidate在vue项目里表单校验应用案例
- 10-1 JS案例:表单校验
- 案例15-基本的表单校验使用validate
- [置顶] 在HTML表单中利用JavaScript实现图片上传的前端校验
- VeeValidate在vue项目里表单校验应用案例
- 简单的前端表单的校验
- 前端表单校验异常validate校验插件异常
- 【JavaScript】案例一:使用JS完成注册页面表单校验——事件(onsubmit&onfocus&onblur)...
- 网上商城案例--表单校验
- 前端学习(3):javavScript权威指南 第八章 表单、表单时间及校验
- 在某网课学习前端笔记整理css篇11-表单元素校验伪类、倒影、布局
- DHTML技术演示---注册表单的验证且控制提交--前端校验(模拟网上账号注册)
- 表单校验经典案例
- 前端校验表单方法
- JEECG框架-如何使用前端代码判重?( duplicateCheck 表单字段重复校验通用JS)
- SSM权限管理-----增删改查对话框的一些前端控制(提交表单)
- web前端案例-开发3D拖拽照片墙
- 前端学习(导航栏案例)