jQuery 最小表单字段验证插件 Validin

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

简介

Validin是一个轻量级、易于使用的jQuery插件,用于使用正则表达式验证各种类型的表单字段。

内置验证规则:

  • 阿尔法:仅限字母
  • 字母数字:字母和数字
  • 字母空间:字母
  • 字母_日期:字母、下划线和连字符
  • 字母数字日期:字母、数字、下划线和连字符
  • 数字:有效整数
  • 十进制的:有效编号
  • 名称:有效名称
  • 电子邮件:有效的电子邮件地址
  • url:有效URL
  • 电话:有效电话号码
  • 拉链:有效的邮政编码
  • 信用卡:有效信用卡号
  • 最小值:至少%i
  • ma:不超过%i
  • 最小长度:至少有%i个字符长
  • 最大长度:长度不超过%i个字符
  • 火柴:这些值必须匹配

如何使用它:

1.要使用此插件,您首先需要在加载jQuery库后加载JavaScript文件“validin.js”:

  1. <script src="//code.jquery.com/jquery.min.js"></script>
  2. <script src="validin.js"></script>

2.调用目标表单元素上的函数,jQuery Validatin就可以使用了。

  1. $('form').validin();

3.添加validate=“规则名称”属性到表单字段,如下所示:

  1. <form>
  2. <input required>
  3. <input validate="alpha">
  4. <input validate="alpha_num">
  5. <input validate="alpha_space">
  6. <input validate="alpha_dash">
  7. <input validate="alpha_num_dash">
  8. <input validate="number">
  9. <input validate="decimal">
  10. <input validate="name">
  11. <input validate="email">
  12. <input validate="url">
  13. <input validate="phone">
  14. <input validate="zip">
  15. <input validate="creditcard">
  16. <input validate="min:5">
  17. <input validate="max:5">
  18. <input validate="min_length:5">
  19. <input validate="max_length:5">
  20. <input class="must_match">
  21. <input validate="match:.must_match">
  22. <input type="submit">
  23. </form>

3.您也可以使用regex定义自己的验证规则。

  1. <input validate="regex:/[0-9a-z]{1,3}-\d*/i">

4.在无效时设置错误消息和表单字段的样式。

  1. input.invalid { border-color: #ff7a7a; }
  2.  
  3. .validation_error {
  4. margin: .4em 0 1em;
  5. color: #ff7a7a;
  6. font-size: .7em;
  7. text-transform: uppercase;
  8. letter-spacing: .15em;
  9. }

5.自定义默认验证规则和错误消息。

  1. $('form').validin({
  2. validation_tests: {
  3. 'alpha': {
  4. 'regex': /[a-zA-Z]*/,
  5. 'error_message': "This can only contain only letters"
  6. },
  7. 'alpha_num': {
  8. 'regex': /[A-Z0-9]*/i,
  9. 'error_message': "This can only contain letters and numbers"
  10. },
  11. 'alpha_space': {
  12. 'regex': /[A-Z ]*/i,
  13. 'error_message': "This can only contain letters"
  14. },
  15. 'alpha_dash': {
  16. 'regex': /[A-Z\.\-_]*/i,
  17. 'error_message': "This can only contain letters, underscores and hyphens"
  18. },
  19. 'alpha_num_dash': {
  20. 'regex': /[A-Z0-9\.\-_]*/i,
  21. 'error_message': "This can only contain letters, numbers, underscores and hyphens"
  22. },
  23. 'number': {
  24. 'regex': /[\d]*/,
  25. 'error_message': "This needs to be a valid whole number"
  26. },
  27. 'decimal': {
  28. 'regex': /(\d*\.?\d*)/,
  29. 'error_message': "This needs to be a valid number"
  30. },
  31. 'name': {
  32. 'regex': /[A-Z\.\-'\s]*/i,
  33. 'error_message': "This needs to be a valid name"
  34. },
  35. 'email': {
  36. 'regex': /[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}/i,
  37. 'error_message': "This needs to be a valid email address"
  38. },
  39. 'url': {
  40. 'regex': /(https?|ftp):\/\/[^\s\/$.?#].[^\s]*/i,
  41. 'error_message': "This needs to be a valid URL"
  42. },
  43. 'phone': {
  44. 'regex': /(?=.*?\d{3}( |-|.)?\d{4})((?:\+?(?:1)(?:\1|\s*?))?(?:(?:\d{3}\s*?)|(?:\((?:\d{3})\)\s*?))\1?(?:\d{3})\1?(?:\d{4})(?:\s*?(?:#|(?:ext\.?))(?:\d{1,5}))?)\b/i,
  45. 'error_message': "This needs to be a valid phone number"
  46. },
  47. 'zip': {
  48. 'regex': /\d{5}(?:-?\d{4})?/i,
  49. 'error_message': "This needs to be a valid zip code"
  50. },
  51. 'creditcard': {
  52. 'regex': /(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})/i,
  53. 'error_message': "This needs to be a valid credit card number"
  54. },
  55. 'regex': {
  56. 'regex': /.*/i,
  57. 'error_message': "This is not a valid value"
  58. },
  59. 'min': {
  60. 'regex': /.*/i,
  61. 'error_message': "This number needs to be at least %i"
  62. },
  63. 'max': {
  64. 'regex': /.*/i,
  65. 'error_message': "This number needs to no more than %i"
  66. },
  67. 'min_length': {
  68. 'regex': /.*/i,
  69. 'error_message': "This needs to be at least %i characters long"
  70. },
  71. 'max_length': {
  72. 'regex': /.*/i,
  73. 'error_message': "This needs to be no more than %i characters long"
  74. },
  75. 'match': {
  76. 'regex': /.*/i,
  77. 'error_message': "These values have to match"
  78. }
  79. },
  80. });

6.更多具有默认值的配置选项。

  1. $('form').validin({
  2.  
  3. // delay time in milliseconds
  4. feedback_delay: 700,
  5.  
  6. // default CSS classes
  7. invalid_input_class: 'invalid',
  8. error_message_class: "validation_error",
  9.  
  10. // default error messages
  11. form_error_message: "Please fix any errors in the form",
  12. required_fields_initial_error_message: "Please fill in all required fields",
  13. required_field_error_message: "This field is required",
  14.  
  15. // adjust margins on the validation error messages
  16. override_input_margins: true,
  17.  
  18. // additional validate rules
  19. tests: {},
  20.  
  21. // callback
  22. onValidateInput: function() {}
  23. });

更新日志:

2022-03-01

  • 改进了文本区域处理(允许回车),限定了更多变量的范围,取代了$jQuery引用

2022-02-28

  • 对不太常见的输入进行了一些可靠性改进和小更新

2021-03-28

  • 修复了用户在输入字段中按Enter键时强制提交的错误

2019-01-01

  • 分离默认选项,更改函数名称以对名称空间进行排序,修复代码中的错误,添加徽标svg

2017-12-14

  • 添加了对复选框和单选类型输入的支持(使用新的getValue()函数),添加了错误消息语言作为可配置选项,更新了自述文件和演示页面内容

2017-09-16

  • 修复了用户可以使用“回车”键提交无效表单的问题,更好地处理一个页面上的多个表单,“匹配”元素现在在值不相同时检查并标记两个字段,如果存在所需字段,提交键会给出不启用的理由,显着更新了演示页面

预览截图