validate.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. /**
  11. * Created by PanJiaChen on 16/11/18.
  12. */
  13. const baseAttr = {
  14. min: '%s最小长度为:min',
  15. max: '%s最大长度为:max',
  16. length: '%s长度必须为:length',
  17. range: '%s长度为:range',
  18. pattern: '$s格式错误',
  19. };
  20. /**
  21. * @param {string} path
  22. * @returns {Boolean}
  23. */
  24. export function isExternal(path) {
  25. return /^(https?:|mailto:|tel:)/.test(path);
  26. }
  27. /**
  28. * @param {string} str
  29. * @returns {Boolean}
  30. */
  31. export function validUsername(str) {
  32. const valid_map = ['admin', 'editor'];
  33. return valid_map.indexOf(str.trim()) >= 0;
  34. }
  35. /**
  36. * @param {string} url
  37. * @returns {Boolean}
  38. */
  39. export function validURL(url) {
  40. const reg =
  41. /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  42. return reg.test(url);
  43. }
  44. /**
  45. * @param {string} str
  46. * @returns {Boolean}
  47. */
  48. export function validLowerCase(str) {
  49. const reg = /^[a-z]+$/;
  50. return reg.test(str);
  51. }
  52. /**
  53. * @param {string} str
  54. * @returns {Boolean}
  55. */
  56. export function validUpperCase(str) {
  57. const reg = /^[A-Z]+$/;
  58. return reg.test(str);
  59. }
  60. /**
  61. * @param {string} str
  62. * @returns {Boolean}
  63. */
  64. export function validAlphabets(str) {
  65. const reg = /^[A-Za-z]+$/;
  66. return reg.test(str);
  67. }
  68. /**
  69. * @param {string} email
  70. * @returns {Boolean}
  71. */
  72. export function validEmail(email) {
  73. // eslint-disable-next-line no-useless-escape
  74. const reg =
  75. /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  76. return reg.test(email);
  77. }
  78. /**
  79. * @param {string} str
  80. * @returns {Boolean}
  81. */
  82. export function isString(str) {
  83. if (typeof str === 'string' || str instanceof String) {
  84. return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * @param {Array} arg
  90. * @returns {Boolean}
  91. */
  92. export function isArray(arg) {
  93. if (typeof Array.isArray === 'undefined') {
  94. return Object.prototype.toString.call(arg) === '[object Array]';
  95. }
  96. return Array.isArray(arg);
  97. }
  98. const bindMessage = (fn, message) => {
  99. fn.message = (field) => message.replace('%s', field || '');
  100. };
  101. export function required(message, opt = {}) {
  102. return {
  103. required: true,
  104. message,
  105. type: 'string',
  106. ...opt,
  107. };
  108. }
  109. bindMessage(required, '请输入%s');
  110. /**
  111. * 正确的金额
  112. *
  113. * @param message
  114. * @returns {*}
  115. */
  116. export function num(message) {
  117. return attrs.pattern(/(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message);
  118. }
  119. bindMessage(num, '%s格式不正确');
  120. const attrs = Object.keys(baseAttr).reduce((attrs, key) => {
  121. attrs[key] = (attr, message = '', opt = {}) => {
  122. const _attr = key === 'range' ? { min: attr[0], max: attr[1] } : { [key]: attr };
  123. return {
  124. message: message.replace(`:${key}`, key === 'range' ? `${attr[0]}-${attr[1]}` : attr),
  125. type: 'string',
  126. ..._attr,
  127. ...opt,
  128. };
  129. };
  130. bindMessage(attrs[key], baseAttr[key]);
  131. return attrs;
  132. }, {});
  133. export default attrs;
  134. /**
  135. * 函数防抖 (只执行最后一次点击)
  136. * @param fn
  137. * @param delay
  138. * @returns {Function}
  139. * @constructor
  140. */
  141. export const Debounce = (fn, t) => {
  142. const delay = t || 500;
  143. let timer;
  144. return function () {
  145. const args = arguments;
  146. if (timer) {
  147. clearTimeout(timer);
  148. }
  149. timer = setTimeout(() => {
  150. timer = null;
  151. fn.apply(this, args);
  152. }, delay);
  153. };
  154. };