bootstrap:能够增加兼容性的强大框架.

因为项目需要数据验证,看bootstrapValidator 还不错,就上手一直,完美兼容,话不多说。

需要引用css:

bootstrap.min.css

bootstrapValidator.min.css

js:

jquery-1.10.2.min.js

bootstrap.min.js

bootstrapValidator.min.js

(下载实例)

以上这些都是必须的。

先上个简单的例子,只要导入相应的文件可以直接运行:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Using Ajax to submit data</title>
  5.  
  6. <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
  7. <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
  8.  
  9. <script type="text/javascript" src="../vendor/jquery/jquery-1.10.2.min.js"></script>
  10. <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
  11. <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <!-- class都是bootstrap定义好的样式,验证是根据input中的name值 -->
  16. <form id="defaultForm" method="post" class="form-horizontal" action="ajaxSubmit.php">
  17. <!-- 下面这个div必须要有,插件根据这个进行添加提示 -->
  18. <div class="form-group">
  19. <label class="col-lg-3 control-label">Username</label>
  20. <div class="col-lg-5">
  21. <input type="text" class="form-control" name="username" />
  22. </div>
  23. </div>
  24.  
  25. <div class="form-group">
  26. <label class="col-lg-3 control-label">Email address</label>
  27. <div class="col-lg-5">
  28. <input type="text" class="form-control" name="email" />
  29. </div>
  30. </div>
  31.  
  32. <div class="form-group">
  33. <label class="col-lg-3 control-label">Password</label>
  34. <div class="col-lg-5">
  35. <input type="password" class="form-control" name="password" />
  36. </div>
  37. </div>
  38.  
  39. <div class="form-group">
  40. <div class="col-lg-9 col-lg-offset-3">
  41. <button type="submit" class="btn btn-primary">Sign up</button>
  42. </div>
  43. </div>
  44. </form>
  45.  
  46. </div>
  47. <script type="text/javascript">
  48. $(document).ready(function() {
  49. /**
  50. * 下面是进行插件初始化
  51. * 你只需传入相应的键值对
  52. * */
  53. $('#defaultForm').bootstrapValidator({
  54. message: 'This value is not valid',
  55. feedbackIcons: {/*输入框不同状态,显示图片的样式*/
  56. valid: 'glyphicon glyphicon-ok',
  57. invalid: 'glyphicon glyphicon-remove',
  58. validating: 'glyphicon glyphicon-refresh'
  59. },
  60. fields: {/*验证*/
  61. username: {/*键名username和input name值对应*/
  62. message: 'The username is not valid',
  63. validators: {
  64. notEmpty: {/*非空提示*/
  65. message: '用户名不能为空'
  66. },
  67. stringLength: {/*长度提示*/
  68. min: 6,
  69. max: 30,
  70. message: '用户名长度必须在6到30之间'
  71. }/*最后一个没有逗号*/
  72. }
  73. },
  74. password: {
  75. message:'密码无效',
  76. validators: {
  77. notEmpty: {
  78. message: '密码不能为空'
  79. },
  80. stringLength: {
  81. min: 6,
  82. max: 30,
  83. message: '用户名长度必须在6到30之间'
  84. }
  85. }
  86. },
  87. email: {
  88. validators: {
  89. notEmpty: {
  90. message: 'The email address is required and can\'t be empty'
  91. },
  92. emailAddress: {
  93. message: 'The input is not a valid email address'
  94. }
  95. }
  96. }
  97. }
  98. });
  99. });
  100. </script>
  101. </body>
  102. </html>

这是最基本的,例子直接复制到本地,并且导入需要的css和js文件(JS中username,password等键值名和input标签中name属性值对应),运行就能够进行非空,长度验证,完全不需要管css样式。

效果图如下:

当然,以上都是插件写好的规则,如果想自己加匹配规则怎么办呢?

如下只要在input相对应的键值中加入一个regexp:{}键值对(在上面的js基础上修改)

  1. username: {/*键名和input name值对应*/
  2. message: 'The username is not valid',
  3. validators: {
  4. notEmpty: {/*非空提示*/
  5. message: '用户名不能为空'
  6. },
  7. regexp: {/* 只需加此键值对,包含正则表达式,和提示 */
  8. regexp: /^[a-zA-Z0-9_\.]+$/,
  9. message: '只能是数字和字母_.'
  10. },
  11. stringLength: {/*长度提示*/
  12. min: 6,
  13. max: 30,
  14. message: '用户名长度必须在6到30之间'
  15. }/*最后一个没有逗号*/
  16. }
  17. },

效果如下:

至此只要运行和看了例子,就能进行大部分的验证了,是不是很简单?只要写相应的键值对即可,再也自己什么都写了。下面进一步的使用,进行用户的注册,

需求:

实时验证用户名是否存在,密码不能和用户名相同,两次密码需要相同,提交之后需要验证返回值

html代码(直接替换上例子中的form即可):

  1. <form id="defaultForm" role="form" class="form-signin" action="registerAccount.do"
  2. method="post">
  3. <h2 class="form-signin-heading">请输入注册信息:</h2>
  4.  
  5. <div class="form-group">
  6. <label for="username">用户名:</label><input class="form-control"
  7. type="text" name="username" id="username" />
  8. </div>
  9. <div class="form-group">
  10. <label for="password">密码:</label><input class="form-control"
  11. type="password" name="password" id="password"/>
  12. </div>
  13. <div class="form-group">
  14. <label for="repassword">确认密码:</label><input class="form-control"
  15. type="password" name="repassword" id="repassword" />
  16. </div>
  17. <div class="form-group">
  18. <label for="phone">手机号码:</label><input class="form-control"
  19. type="text" name="phone" id="phone" />
  20. </div>
  21. <div class="form-group">
  22. <label for="email">email:</label><input class="form-control"
  23. type="email" name="email" id="email" />
  24. </div>
  25. <div class="form-group">
  26. <label for="invite">邀请码:</label><input class="form-control"
  27. type="text" name="invite" id="invite">
  28. </div>
  29. <div class="form-group">
  30. <button class="btn btn-lg btn-primary btn-block" type="submit">确认注册</button>
  31. <a class="btn btn-lg btn-primary btn-block" href="../">返回首页</a>
  32. </div>
  33. </form>

js代码(直接替换例子中的JS):

  1. $(function(){/* 文档加载,执行一个函数*/
  2. $('#defaultForm')
  3. .bootstrapValidator({
  4. message: 'This value is not valid',
  5. feedbackIcons: {/*input状态样式图片*/
  6. valid: 'glyphicon glyphicon-ok',
  7. invalid: 'glyphicon glyphicon-remove',
  8. validating: 'glyphicon glyphicon-refresh'
  9. },
  10. fields: {/*验证:规则*/
  11. username: {//验证input项:验证规则
  12. message: 'The username is not valid',
  13.  
  14. validators: {
  15. notEmpty: {//非空验证:提示消息
  16. message: '用户名不能为空'
  17. },
  18. stringLength: {
  19. min: 6,
  20. max: 30,
  21. message: '用户名长度必须在6到30之间'
  22. },
  23. threshold : 6 , //有6字符以上才发送ajax请求,(input中输入一个字符,插件会向服务器发送一次,设置限制,6字符以上才开始)
  24. remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值,获得一个json数据。例表示正确:{"valid",true}
  25. url: 'exist2.do',//验证地址
  26. message: '用户已存在',//提示消息
  27. delay : 2000,//每输入一个字符,就发ajax请求,服务器压力还是太大,设置2秒发送一次ajax(默认输入一个字符,提交一次,服务器压力太大)
  28. type: 'POST'//请求方式
  29. /**自定义提交数据,默认值提交当前input value
  30. * data: function(validator) {
  31. return {
  32. password: $('[name="passwordNameAttributeInYourForm"]').val(),
  33. whatever: $('[name="whateverNameAttributeInYourForm"]').val()
  34. };
  35. }
  36. */
  37. },
  38. regexp: {
  39. regexp: /^[a-zA-Z0-9_\.]+$/,
  40. message: '用户名由数字字母下划线和.组成'
  41. }
  42. }
  43. },
  44. password: {
  45. message:'密码无效',
  46. validators: {
  47. notEmpty: {
  48. message: '密码不能为空'
  49. },
  50. stringLength: {
  51. min: 6,
  52. max: 30,
  53. message: '用户名长度必须在6到30之间'
  54. },
  55. identical: {//相同
  56. field: 'password', //需要进行比较的input name值
  57. message: '两次密码不一致'
  58. },
  59. different: {//不能和用户名相同
  60. field: 'username',//需要进行比较的input name值
  61. message: '不能和用户名相同'
  62. },
  63. regexp: {
  64. regexp: /^[a-zA-Z0-9_\.]+$/,
  65. message: 'The username can only consist of alphabetical, number, dot and underscore'
  66. }
  67. }
  68. },
  69. repassword: {
  70. message: '密码无效',
  71. validators: {
  72. notEmpty: {
  73. message: '用户名不能为空'
  74. },
  75. stringLength: {
  76. min: 6,
  77. max: 30,
  78. message: '用户名长度必须在6到30之间'
  79. },
  80. identical: {//相同
  81. field: 'password',
  82. message: '两次密码不一致'
  83. },
  84. different: {//不能和用户名相同
  85. field: 'username',
  86. message: '不能和用户名相同'
  87. },
  88. regexp: {//匹配规则
  89. regexp: /^[a-zA-Z0-9_\.]+$/,
  90. message: 'The username can only consist of alphabetical, number, dot and underscore'
  91. }
  92. }
  93. },
  94. email: {
  95. validators: {
  96. notEmpty: {
  97. message: '邮件不能为空'
  98. },
  99. emailAddress: {
  100. message: '请输入正确的邮件地址如:123@qq.com'
  101. }
  102. }
  103. },
  104. phone: {
  105. message: 'The phone is not valid',
  106. validators: {
  107. notEmpty: {
  108. message: '手机号码不能为空'
  109. },
  110. stringLength: {
  111. min: 11,
  112. max: 11,
  113. message: '请输入11位手机号码'
  114. },
  115. regexp: {
  116. regexp: /^1[3|5|8]{1}[0-9]{9}$/,
  117. message: '请输入正确的手机号码'
  118. }
  119. }
  120. },
  121. invite: {
  122. message: '邀请码',
  123. validators: {
  124. notEmpty: {
  125. message: '邀请码不能为空'
  126. },
  127. stringLength: {
  128. min: 8,
  129. max: 8,
  130. message: '请输入正确长度的邀请码'
  131. },
  132. regexp: {
  133. regexp: /^[\w]{8}$/,
  134. message: '请输入正确的邀请码(包含数字字母)'
  135. }
  136. }
  137. },
  138. }
  139. })
  140. .on('success.form.bv', function(e) {//点击提交之后
  141. // Prevent form submission
  142. e.preventDefault();
  143.  
  144. // Get the form instance
  145. var $form = $(e.target);
  146.  
  147. // Get the BootstrapValidator instance
  148. var bv = $form.data('bootstrapValidator');
  149.  
  150. // Use Ajax to submit form data 提交至form标签中的action,result自定义
  151. $.post($form.attr('action'), $form.serialize(), function(result) {
  152. //do something...
  153. });
  154. });
  155. });

效果图:

异常:

Uncaught RangeError: Maximum call stack size exceedede

没有加class="form-group"

基于jquery,bootstrap数据验证插件bootstrapValidator 教程的更多相关文章

  1. 【转】基于jquery,bootstrap数据验证插件bootstrapValidator 教程

    bootstrap:能够增加兼容性的强大框架. 因为项目需要数据验证,看bootstrapValidator 还不错,就上手一直,完美兼容,话不多说. 需要引用css: bootstrap.min.c ...

  2. 基于jquery,bootstrap数据验证插件bootstrapValidator

    bootstrap:能够增加兼容性的强大框架. 因为项目需要数据验证,看bootstrapValidator 还不错,就上手一直,完美兼容,话不多说. 需要引用css: bootstrap.min.c ...

  3. 基于jquery、bootstrap的数据验证插件bootstrapValidator使用

    实时验证用户名是否存在,密码不能和用户名相同,两次密码需要相同,提交之后需要验证返回值: <form id="defaultForm" role="form&quo ...

  4. jquery数据验证插件

    jquery数据验证插件(自制,简单,练手)   一:最近项目中js数据验证比较多,为了统一风格,移植复用,于是顺手封装了Jquery的插件. (function($) { var defaults ...

  5. 出位的template.js 基于jquery的模板渲染插件

    找了好几款基于jquery的模板渲染插件,无一感觉很难用(教程较少.绑定不统一),也可能我智商问题,比如jquery template.js .jtemplate.js. 然后在github上找到这一 ...

  6. 源码来袭!!!基于jquery的ajax分页插件(demo+源码)

    前几天打开自己的博客园主页,无意间发现自己的园龄竟然有4年之久了.可是看自己的博客列表却是空空如也,其实之前也有写过,但是一直没发布(然而好像并没有什么卵用).刚开始学习编程时就接触到博客园,且在博客 ...

  7. 一款基于jQuery的QQ表情插件

    我们在QQ聊天或者发表评论.微博时,会有一个允许加入表情的功能,点击表情按钮,会弹出一系列表情小图片,选中某个表情图片即可发表的丰富的含表情的内容.今天和大家分享一款基于jQuery的QQ表情插件,您 ...

  8. 【jquery】Validform,一款不错的 jquery 表单验证插件

    关于 Validform 这是一款很不错的 jquery 表单验证插件,它几乎能够满足任何验证需求,仅仅一行代码就能搞定整站的表单验证. $('form').Validform(); 为什么能如此方便 ...

  9. 【jQuery基础学习】06 jQuery表单验证插件-Validation

    jQuery的基础部分前面都讲完了,那么就看插件了. 关于jQuery表单验证插件-Validation validation特点: 内置验证规则:拥有必填.数字.E-Mail.URL和信用卡号码等1 ...

随机推荐

  1. POJ 2352 Stars(树状数组)题解

    Language:Default Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52268 Accepted: 22 ...

  2. hdu 3415 Max Sum of Max-K-sub-sequence 单调队列优化DP

    题目链接: https://www.cnblogs.com/Draymonder/p/9536681.html 同上一篇文章,只是 需要记录最大值的开始和结束的位置 #include <iost ...

  3. BZOJ1297: [SCOI2009]迷路 矩阵快速幂

    Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...

  4. Pyinstaller 中 pandas出错问题的解决(详细)

    环境配置 pip install pyinstaller pyinstaller中的参数 -F 表示生成单个可执行文件 -c 显示命令行窗口,一般一开始的时候使用,如果没有错误,那就使用-w命令 -w ...

  5. HDU 6069 Counting Divisors(唯一分解定理+因子数)

    http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意: 思路: 根据唯一分解定理,$n={a_{1}}^{p1}*{a2_{}}^{p2}...*{a_{ ...

  6. List与数组的相互转换

    1.从string[]转List<string> string[] str={“1”,”2”}; List <string> list=new List<string&g ...

  7. #使用ListView更新数据出现闪烁解决办法

    //使用双缓冲:添加新类继承ListView 对其重写 public class DoubleBufferListView : ListView { public DoubleBufferListVi ...

  8. 非[无]root权限 服务器 下安装perl以及perl模块

    转载自http://www.zilhua.com 在本博客中,所有的软件安装都在服务器上,且无root权限.理论上适合所有的用户. 我的安装目录 cd /home/zilhua/software 1. ...

  9. C++ Boost在VS2015中的使用

    1.下载包 目录结构: 切换到上面的目录,然后运行 bootstrap.bat 执行完毕后会生成两个exe文件 继续执行 bjam.exe 结束后,目录如下 2.设置路径 测试 #include &q ...

  10. R-CNN(Rich feature hierarchies for accurate object detection and semantic segmentation)论文理解

    论文地址:https://arxiv.org/pdf/1311.2524.pdf 翻译请移步: https://www.cnblogs.com/xiaotongtt/p/6691103.html ht ...