validate.js 4.3 KB

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