强大 多语言表单验证插件 jQuery Nice Validator

  • 源码大小:272.69KB
  • 所需积分:1积分
  • 源码编号:19JP-3404
  • 浏览次数:847次
  • 最后更新:2023年06月09日
  • 所属栏目:表单
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

还有另一个jQuery支持的表单验证器,用于使用自定义验证规则验证表单字段。

特征:

  • 多种语言(en、ja、zh-cn、zh-tw)。
  • 使用Regex或JS函数的自定义规则。
  • 支持客户端和服务器端验证。
  • 完全可定制,易于扩展。

目录:

  • 验证规则
  • 选项
  • API方法
  • 事件

如何使用它:

1.下载并加载Nice Validator插件。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  2. <script src="/path/to/dist/jquery.validator.min.js?local=en"></script>

2.使用验证表单字段数据-*属性:

  • ;-以及
  • :-将字段与规则分开
  • ()-用于传递规则参数
  • ,-用于分隔多个规则
  • ~-范围
  • &-以及
  • !-不等于
  • |-或
  1. <form class="form" data-validator-option="{timely:2, focusCleanup:true}">
  2. <div class="form-item">
  3. <label class="label">Username</label>
  4. <input type="text" name="username"
  5. data-rule="required;username;"
  6. data-rule-username="[/^[\w\d]{3,12}$/, 'Please enter 3-12 digits, letters, underscores']"
  7. data-tip="You can use letters, numbers and periods"
  8. >
  9. </div>
  10. <div class="form-item">
  11. <label class="label">Password</label>
  12. <input type="password" name="pwd"
  13. data-rule="Password: required; length(8~16)"
  14. data-tip="Please fill in at least eight characters"
  15. >
  16. </div>
  17. <div class="form-item">
  18. <label class="label">Verify Password</label>
  19. <input type="password" name="pwdAgain" data-rule="Verify Password: required; match(pwd)">
  20. </div>
  21. <div class="form-item">
  22. <label class="label">Gender</label>
  23. <select name="gender" data-rule="required">
  24. <option value="">select ...</option>
  25. <option value="1">Male</option>
  26. <option value="2">Female</option>
  27. <option value="3">Other</option>
  28. </select>
  29. </div>
  30. <div class="form-item">
  31. <label class="label">Email</label>
  32. <input type="text" name="email" data-rule="required;email">
  33. </div>
  34. <div class="form-item">
  35. <label class="label">Interest</label>
  36. <label><input type="checkbox" name="interest" data-rule="checked">sports</label>
  37. <label><input type="checkbox" name="interest">movie</label>
  38. <label><input type="checkbox" name="interest">game</label>
  39. </div>
  40. <div class="form-item">
  41. <label class="label">Note</label>
  42. <textarea data-rule="required;"></textarea>
  43. </div>
  44. <div class="form-item">
  45. <label><input type="checkbox" id="agree_arguments" data-rule="checked">I agree to the service agreement.</label>
  46. </div>
  47. <div class="form-item">
  48. <button type="submit">Submit</button>
  49. </div>
  50. </form>

3.或者通过JavaScript。

  1. $('#form').validator({
  2. fields: {
  3. 'email': 'required;email',
  4. 'pwd': 'required;length(6~16)',
  5. // ...
  6. }
  7. });

4.内置验证规则:

  • 必修的:必填字段
  • 选中的:已检查(n),已检查(n~),已检测(~n),检测(n1~n2)
  • 匹配:match(name),match(neq,name),匹配(lt,name)、匹配(gt,name)和匹配(lte,name)
  • 远程:remote(URL), remote(get:URL), remote(URL, name1, #id2 ...), remote(URL, foo=value1&bar=value2, name3 ...), remote(URL, foo:name1, bar:#id2, name3 ...), remote(jsonpURL), remote(jsonp:URL), remote(cors:post:URL)
  • 整数:integer(+), integer(+0), integer(-), integer(-0)
  • 范围:范围(n~),范围(~n),范围
  • 长度:长度(n),长度(n~),长度
  • 过滤器:筛选器(RegExp)

5.创建自定义规则:

  1. // en.js
  2. $.validator.config({
  3. // Custom rules
  4. rules: {
  5. // ...
  6. },
  7. });
  1. // or
  2. $("#form").validator(
  3. rules: {
  4. // ...
  5. }
  6. );
  1. // or via data attribute
  2. <input name="demo" data-rule="required;xxx" data-rule-xxx="[/^\d{6}$/, '6 digits']">
  1. // or via JS function
  2. mobile: function(element, params) {
  3. return /^1[3458]\d{9}$/.test(element.value) || 'Phone Number Validator';
  4. };
  5.  
  6. // using test function
  7. loginName: function(element) {
  8. return /^[a-zA-Z]\w{3,}/.test(element.value) ||
  9. this.test(element, "mobile")===true ||
  10. this.test(element, "email")===true ||
  11. 'Input username, phone number, or email';
  12. }
  13.  
  14. // custom ajax validation
  15. myRemote: function(element){
  16. return $.ajax({
  17. url: 'check/username.php',
  18. type: 'post',
  19. data: element.name +'='+ element.value,
  20. dataType: 'json'
  21. });
  22. }

6.可用的插件选项:

  1. $("#form").validator(
  2. // 1 = enable debug
  3. // 2 = ignore validation result
  4. debug: 0,
  5. // 0: validate the form on submit
  6. // 1: validate the field when lose focus
  7. // 2: validate the field when the value has changed
  8. // 3: 1+2
  9. // 8: validate the field when the value has changed and show the result
  10. // 9: 1+2 and show the result
  11. timely: 1,
  12. // theme name
  13. theme: 'default',
  14. // ignore form fields
  15. ignore: '',
  16. // ignore empty fields
  17. ignoreBlank: false,
  18. // move focus to the first invalid field
  19. focusInvalid: true,
  20. // clear error message when the field got focus
  21. focusCleanup: false,
  22. // stop the plugin when the first error is captured
  23. stopOnError: false,
  24. // default CSS classes
  25. formClass: 'n-default',
  26. validClass: 'n-valid',
  27. invalidClass: 'n-invalid',
  28. bindClassTo: null,
  29. // show success messages
  30. showOk: true,
  31. // custom error messages
  32. msgWrapper: 'span',
  33. msgArrow: '',
  34. msgIcon: '<span class="n-icon"></span>',
  35. msgClass: 'n-right',
  36. msgStyle: '',
  37. messages: {
  38. required: "Please fill in this field",
  39. email: "Please enter a valid email address.",
  40. // ...
  41. },
  42. msgMaker: function(opt) {
  43. var html;
  44. html = '<span role="alert" class="msg-wrap n-'+ opt.type + '">' + opt.arrow;
  45. if (opt.result) {
  46. $.each(opt.result, function(i, obj){
  47. html += '<span class="n-'+ obj.type +'">' + opt.icon + '<span class="n-msg">' + obj.msg + '</span></span>';
  48. });
  49. } else {
  50. html += opt.icon + '<span class="n-msg">' + opt.msg + '</span>';
  51. }
  52. html += '</span>';
  53. return html;
  54. },
  55. // custom display
  56. display: function(elem) {
  57. // ...
  58. },
  59. // function(elem) | jqSelector
  60. target: null,
  61. // apply options & validators to fields
  62. fileds: {
  63. // ...
  64. }
  65. // translate ajax response to validation result
  66. dataFilter: function (data) {
  67. if ( isString(data) || ( isObject(data) && ('error' in data || 'ok' in data) ) ) {
  68. return data;
  69. }
  70. },
  71. // callbacks
  72. beforeSubmit: function(form) {
  73. // ...
  74. },
  75. valid: function(form) {
  76. // ...
  77. },
  78. invalid: function(form, errors) {
  79. // ...
  80. },
  81. validation: function(form) {
  82. // ...
  83. },
  84. msgShow: function($msgbox, type) {
  85. // ...
  86. },
  87. msgHide: function($msgbox, type) {
  88. // ...
  89. },
  90. );

7.API方法:

  1. // clear messages
  2. $('#form').validator("cleanUp");
  3.  
  4. // destroy the plugin
  5. $('#form').validator("destroy");
  6.  
  7. // check if is valid
  8. $('#field').isValid(function(v){
  9. if (v) {
  10. // do something
  11. }
  12. });
  13.  
  14. // update options
  15. $.validator.config({
  16. // options here
  17. });
  18.  
  19. // set theme
  20. $.validator.setTheme("myTheme", {
  21. // options here
  22. });
  23.  
  24. // test validation rule
  25. $('#form').test(elem, rule);
  26.  
  27. // set form field
  28. $('#form').setField(key, field);
  29. $('#form')..setField(obj);
  30.  
  31. // show/hide messages
  32. $('#form').showMsg(elem, obj);
  33. $('#form').hideMsg(elem);
  34.  
  35. // prevent duplicate submit
  36. $('#form').holdSubmit(hold);

8.事件:

  1. $('#form').on('validation', function(e, current){
  2. // do something...
  3. });
  4.  
  5. $('#form').on('valid.form', function(e, form){
  6. // do something...
  7. });
  8.  
  9. $('#form').on('invalid.form', function(e, form, errors){
  10. // do something...
  11. });
  12.  
  13. $('#username').on('valid.field', function(e, result){
  14. // do something...
  15. });
  16.  
  17. $('#username').on('invalid.field', function(e, result){
  18. // do something...
  19. });
  20.  
  21. $('#username').on('valid.rule', function(e, ruleName){
  22. if (ruleName === 'remote') {
  23. // do something...
  24. }
  25. });
  26.  
  27. $('#username').on('invalid.rule', function(e, ruleName){
  28. if (ruleName === 'remote') {
  29. // do something...
  30. }
  31. });

预览截图