基于BootstrapValidator的Form表单验证(24)
2016-12-12 00:00
871 查看
Form表单进行数据验证是十分必要的,我们可以自己写JS脚本或者使用JQuery Validate 插件来实现。对于Bootstrap而言,利用BootstrapValidator来做Form表单验证是个相当不错的选择,两者完全兼容,我们也不用去关注CSS样式等美工效果。
0x01 引入BootstrapValidator
官网:BootstrapValidator,作为一个纯粹的使用者,我们可以在上面的链接处下载相关文件并引入,也可以利用CDN方式引入:
<link href="//cdn.bootcss.com/bootstrap-validator/0.5.3/css/bootstrapValidator.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script>
0x02 用户注册实例
下面使用一个用户注册的实例,来总结BootstrapValidator的基本使用方法(其中的JS和CSS文件的引入,请根据自己的实际位置进行调整):
验证效果如下:
0x03 后记
在实际应用中,可能还会遇到类似Ajax提交验证的问题,处理过程是类似的,以后再结合实际的应用来讲这个问题。
类似BootstrapValidator这种基于JS来做验证的过程只是客户端验证,只是为了提高用户体验,并不能保证提交数据的安全性,后端开发者还要做相应的后台验证。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
使用BootStrapValidator完成前端输入验证
利用bootstrapValidator验证UEditor
实用又漂亮的BootstrapValidator表单验证插件
JS组件Form表单验证神器BootstrapValidator
jquery表单验证使用插件formValidator
jQuery formValidator表单验证插件开源了 含API帮助、源码、示例
jQuery表单验证插件formValidator(改进版)
jquery validator 插件增加日期比较方法
formvalidator验证插件中有关ajax验证问题
0x01 引入BootstrapValidator
官网:BootstrapValidator,作为一个纯粹的使用者,我们可以在上面的链接处下载相关文件并引入,也可以利用CDN方式引入:
<link href="//cdn.bootcss.com/bootstrap-validator/0.5.3/css/bootstrapValidator.min.css" rel="stylesheet">
<script src="//cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script>
0x02 用户注册实例
下面使用一个用户注册的实例,来总结BootstrapValidator的基本使用方法(其中的JS和CSS文件的引入,请根据自己的实际位置进行调整):
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>用户注册</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="../../../css/bootstrap.min.css" rel="stylesheet"> <link href="//cdn.bootcss.com/bootstrap-validator/0.5.3/css/bootstrapValidator.min.css" rel="stylesheet"> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <script src="../../../js/bootstrap.min.js"></script> <script src="//cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script> </head> <body> <div class="container col-lg-3 col-lg-offset-3"> <div class="page-header"> <h3>用户注册</h3> </div> <div> <form id="registerForm" method="POST" class="form-horizontal" action="用户注册.html"> <div class="form-group"> <!--注册的用户名--> <label class="control-label" for="username">*请输入注册用户名:</label> <input type="text" class="form-control" placeholder="请输入注册用户名" name="username" id="username"> </div> <div class="form-group"> <!--注册密码--> <label class="control-label" for="password">*请输入注册密码:</label> <input type="password" class="form-control" placeholder="请输入注册密码" name="password" id="password"> </div> <div class="form-group"> <!--确认密码--> <label class="control-label" for="repassword">*请输入确认密码:</label> <input type="password" class="form-control" placeholder="请输入确认密码" name="repassword" id="repassword"> </div> <div class="form-group"> <label class="control-label" for="phone">*请输入手机号码:</label> <input type="text" class="form-control" placeholder="请输入手机号码" name="phone" id="phone"> </div> <div class="form-group"> <label class="control-label" for="email">*请输入电子邮箱:</label> <input type="text" class="form-control" placeholder="请输入电子邮箱" name="email" id="email"> </div> <div class="form-group"> <label class="control-label" for="inviteCode">*请输入邀请码:</label> <input type="text" class="form-control" placeholder="请输入邀请码" name="inviteCode" id="inviteCode"> </div> <div class="form-group"> <button type="submit" class="btn btn-primary form-control">提交注册</button> </div> </form> </div> </div> <script> $(function () { <!--数据验证--> $("#registerForm").bootstrapValidator({ message:'This value is not valid', // 定义未通过验证的状态图标 feedbackIcons: {/*输入框不同状态,显示图片的样式*/ valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, // 字段验证 fields:{ // 用户名 username:{ message:'用户名非法', validators:{ // 非空 notEmpty:{ message:'用户名不能为空' }, // 限制字符串长度 stringLength:{ min:3, max:20, message:'用户名长度必须位于3到20之间' }, // 基于正则表达是的验证 regexp:{ regexp:/^[a-zA-Z0-9_\.]+$/, message:'用户名由数字字母下划线和.组成' } } }, // 密码 password:{ message:'密码非法', validators:{ notEmpty:{ message:'密码不能为空' }, // 限制字符串长度 stringLength:{ min:3, max:20, message:'密码长度必须位于3到20之间' }, // 相同性检测 identical:{ // 需要验证的field field:'password', message:'两次密码输入不一致' }, // 基于正则表达是的验证 regexp:{ regexp:/^[a-zA-Z0-9_\.]+$/, message:'密码由数字字母下划线和.组成' } } }, // 确认密码 repassword:{ message:'密码非法', validators:{ notEmpty:{ message:'密码不能为空' }, // 限制字符串长度 stringLength:{ min:3, max:20, message:'密码长度必须位于3到20之间' }, // 相同性检测 identical:{ // 需要验证的field field:'password', message:'两次密码输入不一致' }, // 基于正则表达是的验证 regexp:{ regexp:/^[a-zA-Z0-9_\.]+$/, message:'密码由数字字母下划线和.组成' } } }, // 电子邮箱 email:{ validators:{ notEmpty:{ message:'邮箱地址不能为空' }, emailAddress:{ message:'请输入正确的邮箱地址' } } }, // 手机号码 phone:{ validators:{ notEmpty:{ message:'手机号码不能为空' }, stringlength:{ min:11, max:11, message:'请输入11位手机号码' }, regexp:{ regexp:/^1[3|5|8]{1}[0-9]{9}$/, message:'请输入正确的手机号码' } } }, // 邀请码 inviteCode:{ validators:{ notEmpty:{ message:'邀请码不能为空' }, stringlength:{ min:9, max:9, message:'请输入9位邀请码' }, regexp:{ regexp:/^[\w]{8}$/, message:'邀请码由数字和字母组成' } } } } }) }) </script> </body> </html>
验证效果如下:
0x03 后记
在实际应用中,可能还会遇到类似Ajax提交验证的问题,处理过程是类似的,以后再结合实际的应用来讲这个问题。
类似BootstrapValidator这种基于JS来做验证的过程只是客户端验证,只是为了提高用户体验,并不能保证提交数据的安全性,后端开发者还要做相应的后台验证。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
bootstrapValidator自定验证方法写法使用BootStrapValidator完成前端输入验证
利用bootstrapValidator验证UEditor
实用又漂亮的BootstrapValidator表单验证插件
JS组件Form表单验证神器BootstrapValidator
jquery表单验证使用插件formValidator
jQuery formValidator表单验证插件开源了 含API帮助、源码、示例
jQuery表单验证插件formValidator(改进版)
jquery validator 插件增加日期比较方法
formvalidator验证插件中有关ajax验证问题
相关文章推荐
- Bootstrap学习总结笔记(24)-- 基于BootstrapValidator的Form表单验证
- 基于BootstrapValidator的Form表单验证(24)
- spring MVC中基于hibernate validator的form表单验证
- sharepoint 基于AD的Form表单身份验证
- spring MVC中基于hibernate validator的form表单验证
- 基于Jquery的FormValidator表单验证插件
- spring MVC中基于hibernate validator的form表单验证
- 基于javascript的Form表单验证
- 基于AD的Form表单身份验证
- 基于jquery的form表单验证
- 基于vue2.0前端组件库element中 el-form表单 自定义验证填坑
- 国人编写的、超牛的、基于jQuery插件式、监听模式(无侵入)的html页面form表单通用验证框架
- 基于 猫冬的 jQuery formValidator表单验证 的asp.net 控件
- Struts2学习笔记3 ――基于AJAX的表单验证
- 在ASP.NET中如何用C#.NET实现基于表单的验证
- 基于prototype的validation.js发布2.3.4新版本,让你彻底脱离表单验证的烦恼
- 在ASP.NET中如何用C#.NET实现基于表单的验证
- 在ASP.NET中如何用C#.NET实现基于表单的验证
- ASP.NET基于表单的验证实现网上安全访问,管理(1)
- 使用AbstractWizardFormController提交复杂表单的验证解决方案