Bootstrap程序5 HTML5表单验证器 jbvalidator

  • 源码大小:207.09KB
  • 所需积分:1积分
  • 源码编号:19JP-3682
  • 浏览次数:845次
  • 最后更新:2023年07月10日
  • 所属栏目:表单
本站默认解压密码:19jp.com 或 19jp_com

简介

jbvalidator是一个全新的基于jQuery的表单验证插件,它是为最新的Bootstrap 5框架创建的,同时支持客户端和服务器端验证。

更多功能:

  • 多种语言。
  • 自定义错误消息。
  • 自定义验证规则。
  • 通过HTML数据属性易于使用。

如何使用它:

1.在文档中加载最新的jQuery JavaScript库和Bootstrap 5框架。

  1. <link rel="stylesheet" href="/path/to/cdn/bootstrap.min.css" />
  2. <script src="/path/to/cdn/jquery.min.js"></script>
  3. <script src="/path/to/cdn/bootstrap.min.js"></script>

2.添加新伐地酸盐属性类型元素,并使用以下HTML将验证器应用于表单字段数据属性:

  • 数据v相等:密码字段的id,其中的值应该相同
  • 数据-最小值-选择:应选择最小数量的选项
  • 数据-最大值-选择:允许选择的最大选项数
  • 数据复选框组:组复选框元素的父属性
  • 数据-最小值-选择:应选择最小复选框数
  • 需要数据v:需要父属性
  • 数据-最小值:最小值
  • 数据v-max:最大值
  • 数据-最小长度:最小长度
  • 数据v-最大长度:最大长度
  • 数据-最小尺寸:最小尺寸
  • 数据v-最大大小:最大尺寸
  • 数据v消息:自定义错误消息
  1. <form class="example" novalidate>
  2. <div class="form-group">
  3. <label>Email</label>
  4. <input type="email" class="form-control" placeholder="[email protected]" required>
  5. </div>
  6. <div class="form-group">
  7. <label for="password">Password</label>
  8. <input type="password" name="password" class="form-control" id="password" title="password" required>
  9. </div>
  10. <div class="form-group">
  11. <label for="password">Confirm Password</label>
  12. <input name="repassword" type="password" class="form-control" data-v-equal="#password" required>
  13. </div>
  14. <div class="form-group">
  15. <label>URL</label>
  16. <input type="url" class="form-control" placeholder="http://www" required>
  17. </div>
  18. <div class="form-group">
  19. <label>Using Regex</label>
  20. <input type="text" class="form-control" pattern="[0-9]+" title="Only number." required>
  21. </div>
  22. <div class="form-group">
  23. <label>Custom Min/Max Values</label>
  24. <input type="text" class="form-control" data-v-min="10" data-v-max="100">
  25. </div>
  26. <div class="form-group">
  27. <label>Custom Min/Max Length</label>
  28. <input type="text" class="form-control" data-v-min-length="5" data-v-max-length="10">
  29. </div>
  30. <div class="form-group">
  31. <label>Multiple Select</label>
  32. <select class="form-select" multiple data-v-min-select="2" data-v-max-select="3">
  33. <option selected>Open this select menu</option>
  34. <option value="1">One</option>
  35. <option value="2">Two</option>
  36. <option value="3">Three</option>
  37. <option value="4">Four</option>
  38. <option value="5">Five</option>
  39. </select>
  40. </div>
  41. <div class="form-group">
  42. <label>Textarea</label>
  43. <textarea class="form-control" minlength="10" maxlength="165"></textarea>
  44. </div>
  45. <div class="form-group">
  46. <div class="form-check">
  47. <input class="form-check-input" type="checkbox" value="" id="defaultCheck1" >
  48. <label class="form-check-label" for="defaultCheck1">
  49. checkbox 1
  50. </label>
  51. </div>
  52. <div class="form-check">
  53. <input class="form-check-input" type="checkbox" value="" id="defaultCheck2" >
  54. <label class="form-check-label" for="defaultCheck2">
  55. checkbox 2
  56. </label>
  57. </div>
  58. <div class="form-check">
  59. <input class="form-check-input" type="checkbox" value="" id="defaultCheck3" >
  60. <label class="form-check-label" for="defaultCheck3">
  61. checkbox 3
  62. </label>
  63. </div>
  64. </div>
  65. <div class="form-group">
  66. <label>File Input</label>
  67. <input type="file" class="form-control" data-v-min-size="400" data-v-max-size="450">
  68. </div>
  69. <div class="form-group">
  70. <label>Date Input</label>
  71. <input type="date" class="form-control" min="2020-10-20">
  72. </div>
  73. <div class="form-group">
  74. <label>Custom message</label>
  75. <input type="text" class="form-control" minlength="10" data-v-message="Please enter minimum 10 characters" required>
  76. </div>
  77. <div class="form-group">
  78. <input type="submit" class="btn btn-primary" value="Submit">
  79. </div>
  80. </form>

3.激活表单验证插件并确定JSON语言的路径。可用语言:

  • 德杰森
  • en.json公司
  • es.json格式
  • fr.json公司
  • tr.json公司
  1. let validator = $('form.example').jbvalidator({
  2. language: 'dist/lang/en.json'
  3. });

4.如您在en.json公司.

  1. {
  2. "maxValue": "You cannot enter a number greater than %s.",
  3. "minValue": "Please enter a number greater than %s.",
  4. "maxLength": "Please use maximum %s character. You are using %s characters.",
  5. "minLength": "Please use minimum %s character, you are using %s characters.",
  6. "minSelectOption": "Please select at least %s options.",
  7. "maxSelectOption": "Please select at most %s options.",
  8. "groupCheckBox": "Please select at least %s options.",
  9. "equal": "This field does not match %s",
  10. "fileMinSize": "File size cannot be less than %s bytes.",
  11. "fileMaxSize": "File size cannot be more than %s bytes.",
  12. "number": "Please write a number."
  13. }

5.示例显示了如何在服务器端验证表单字段。

  1. $(document).on('submit', '.example', function(){
  2. $.ajax({
  3. method:"get",
  4. url:"test.json",
  5. data: $(this).serialize(),
  6. success: function (data){
  7. if(data.status === 'error') {
  8. validatorServerSide.errorTrigger($('[name=username]'), data.message);
  9. }
  10. }
  11. })
  12. return false;
  13. });

6.您也可以使用JavaScript创建自己的验证规则。

  1. validator.validator.custom = function(el, event){
  2. if($(el).is('[name=password]') && $(el).val().length < 5){
  3. return 'Your password is too weak.';
  4. }
  5. }

7.确定是否在有效时将成功应用于表单字段。默认值:false。

  1. let validator = $('form.example').jbvalidator({
  2. language: 'dist/lang/en.json',
  3. successClass: true
  4. });

8.覆盖默认的CSS类。

  1. let validator = $('form.example').jbvalidator({
  2. validFeedBackClass: 'valid-feedbak',
  3. invalidFeedBackClass: 'invalid-feedback',
  4. validClass: 'is-valid',
  5. invalidClass: 'is-invalid'
  6. });

9.API方法。

  1. // add custom validator
  2. validator.validator(validation)
  3.  
  4. // show errors without submitting the form, return error count
  5. validator.checkAll()
  6.  
  7. // show the error messages returned from the server.
  8. validator.errorTrigger():
  9.  
  10. // reload instance after dynamic element is added
  11. validator.reload():

关于作者:

作者:emretulek

网站:https://emretulek.github.io/jbvalidator/

更新日志:

v1.0.9(2022年2月3日)

    • 添加了西班牙语文件es.json

v1.0.8(2022年1月1日)

  • 添加DE语言、德语

1.0.7版(2021 12月12日)

  • 使现代化

1.0.6版(2021 6月10日)

  • 使现代化

1.0.5版(2021 9月15日)

  • 添加了新方法:reload()和checkAll()

1.0.4版(2021 11月6日)

  • 添加法语翻译。

1.0.3版(2021 10月5日)

  • 使现代化

1.0.1版(2021 10月4日)

  • JS已更新

预览截图