表单验证之BootstrapValidator

女爷i 2022-07-14 15:20 321阅读 0赞

Bootstrap Validator是为Bootstrap3设计的一款表单验证jQuery插件,非常适合基于Bootstrap框架的网站。
看作者的github,这款插件已经不再更新了,而推荐使用FormValidation,不过现在还是先介绍一下BootstrapValidator的使用。


准备工作

BootstrapValidator文档地址:http://bv.doc.javake.cn/

下载源码后,将其中的bootstrapValidator.min.cssbootstapValidator.min.js导入项目,并在页面中引入这些组件,如下:

  1. <link rel="stylesheet" type="text/css" href="path/bootstrapValidator.min.css">
  2. <script type="text/javascript" src="path/bootstrapValidator.min.js"></script>

其中path是对应文件导入项目的路径

简单应用

文档中给出调用插件的方法是:

  1. $(document).ready(function() {
  2. $(formSelector).bootstrapValidator({
  3. excluded: ...,
  4. feedbackIcons: ...,
  5. live: 'enabled',
  6. message: 'This value is not valid',
  7. submitButtons: 'button[type="submit"]',
  8. submitHandler: null,
  9. trigger: null,
  10. fields: {
  11. <fieldName>: { enabled: true, message: 'This value is not valid', container: null, selector: null, trigger: null, // Map the validator name with its options validators: { ... <validatorName>: <validatorOptions> ... } } ... } }); });

下面针对一个简单的表单来进行说明:

  1. <form id="logForm" class="form-horizontal">
  2. <div class="form-group">
  3. <label class="col-lg-3 control-label">用户名</label>
  4. <div class="col-lg-5">
  5. <input type="text" class="form-control" name="username" />
  6. </div>
  7. </div>
  8. <div class="form-group">
  9. <label class="col-lg-3 control-label">邮箱</label>
  10. <div class="col-lg-5">
  11. <input type="text" class="form-control" name="email" />
  12. </div>
  13. </div>
  14. <div class="form-group">
  15. <label class="col-lg-3 control-label">密码</label>
  16. <div class="col-lg-5">
  17. <input type="password" class="form-control" name="password" />
  18. </div>
  19. </div>
  20. <button type="submit" class="btn btn-md">提交</button>
  21. </form>

对于上面这个表单应用BootstrapValidator非常简单,fieldName 对应表单中每一项的 name 属性,然后BV还内置了很多 validator 供用户选择,详细可参考文档的 validators 部分,可以看到,邮箱格式的验证正是其中之一,不需要用户自己写正则了。

  1. $(document).ready(function() {
  2. $('#signup-form').bootstrapValidator({
  3. fields: {
  4. username: {
  5. validators: {
  6. notEmpty: {
  7. message: '用户名不能为空'
  8. },
  9. stringLength: {
  10. min: 3,
  11. max: 6,
  12. message: '用户名只能在3-6个字符之间哦~'
  13. }
  14. }
  15. },
  16. email: {
  17. validators: {
  18. emailAddress: {
  19. message: '邮箱格式有误'
  20. }
  21. }
  22. },
  23. password: {
  24. validators: {
  25. notEmpty: {
  26. message: '密码不能为空'
  27. },
  28. stringLength: {
  29. min: 6,
  30. max: 8,
  31. message: '密码必须在6-8个字符之间~'
  32. },
  33. regexp: {
  34. regexp: /^[a-zA-Z0-9]+$/,
  35. message: '密码只能由字母、数字组成~'
  36. }
  37. }
  38. }
  39. }
  40. });
  41. });

不符合验证要求时,会显示错误提示的message,并且禁用提交按钮,提示信息的颜色字体等等都可以重写css来设置,效果展示如下:

注册验证

注:图中的注册按钮处于禁用状态

下面再介绍一下fields的 selector,因为表单数据往往是属于某一个注册用户所有,为方便与后台进行数据交互,我们往往按如下的形式设置name,这时候就不能直接利用name属性来进行验证了,而是使用selector来定义fields:

  1. <form class="form-horizontal">
  2. <div class="form-group">
  3. <label class="col-lg-3 control-label">用户名</label>
  4. <div class="col-lg-5">
  5. <input type="text" id="user" class="form-control" name="login_user.userName" />
  6. </div>
  7. </div>
  8. <div class="form-group">
  9. <label class="col-lg-3 control-label">密码</label>
  10. <div class="col-lg-5">
  11. <input type="password" id="pass" class="form-control" name="login_user.password" />
  12. </div>
  13. </div>
  14. <button type="submit" id="submitBtn" class="btn btn-md">提交</button>
  15. </form>

对应的js代码:

  1. $(document).ready(function() {
  2. $('#signup-form').bootstrapValidator({
  3. fields: {
  4. user: {
  5. selector: '#user',
  6. validators: {
  7. notEmpty: {
  8. message: '用户名不能为空'
  9. },
  10. stringLength: {
  11. min: 3,
  12. max: 6,
  13. message: '用户名只能在3-6个字符之间哦~'
  14. }
  15. }
  16. },
  17. pass: {
  18. selector: '#pass',
  19. validators: {
  20. notEmpty: {
  21. message: '密码不能为空'
  22. },
  23. stringLength: {
  24. min: 6,
  25. max: 8,
  26. message: '密码必须在6-8个字符之间~'
  27. },
  28. regexp: {
  29. regexp: /^[a-zA-Z0-9]+$/,
  30. message: '密码只能由字母、数字组成~'
  31. }
  32. }
  33. }
  34. }
  35. });
  36. });

如果你嫌弃这样写代码累赘,可以直接应用相应的HTML属性,详细可参考文档的 settings 部分

截图2


以上只是BootstrapValidator的一个非常简单的应用, 官方文档 很详细,感兴趣的话就继续查阅,来深入了解它的强大功能吧~

发表评论

表情:
评论列表 (有 0 条评论,321人围观)

还没有评论,来说两句吧...

相关阅读