validate.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 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 = /^(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.,?'\\+&%$#=~_-]+))*$/
  63. return reg.test(url)
  64. }
  65. /**
  66. * @param {string} str
  67. * @returns {Boolean}
  68. */
  69. export function validLowerCase(str) {
  70. const reg = /^[a-z]+$/
  71. return reg.test(str)
  72. }
  73. /**
  74. * @param {string} str
  75. * @returns {Boolean}
  76. */
  77. export function validUpperCase(str) {
  78. const reg = /^[A-Z]+$/
  79. return reg.test(str)
  80. }
  81. /**
  82. * @param {string} str
  83. * @returns {Boolean}
  84. */
  85. export function validAlphabets(str) {
  86. const reg = /^[A-Za-z]+$/
  87. return reg.test(str)
  88. }
  89. /**
  90. * @param {string} email
  91. * @returns {Boolean}
  92. */
  93. export function validEmail(email) {
  94. // eslint-disable-next-line no-useless-escape
  95. const reg = /^(([^<>()\[\]\\.,;:\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,}))$/
  96. return reg.test(email)
  97. }
  98. /**
  99. * @param {string} str
  100. * @returns {Boolean}
  101. */
  102. export function isString(str) {
  103. if (typeof str === 'string' || str instanceof String) {
  104. return true
  105. }
  106. return false
  107. }
  108. /**
  109. * @param {Array} arg
  110. * @returns {Boolean}
  111. */
  112. export function isArray(arg) {
  113. if (typeof Array.isArray === 'undefined') {
  114. return Object.prototype.toString.call(arg) === '[object Array]'
  115. }
  116. return Array.isArray(arg)
  117. }
  118. const bindMessage = (fn, message) => {
  119. fn.message = field => message.replace("%s", field || "");
  120. };
  121. export function required(message, opt = {}) {
  122. return {
  123. required: true,
  124. message,
  125. type: "string",
  126. ...opt
  127. };
  128. }
  129. bindMessage(required, "请输入%s");
  130. /**
  131. * 正确的金额
  132. *
  133. * @param message
  134. * @returns {*}
  135. */
  136. export function num(message) {
  137. return attrs.pattern(
  138. /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
  139. message
  140. );
  141. }
  142. bindMessage(num, "%s格式不正确");
  143. const attrs = Object.keys(baseAttr).reduce((attrs, key) => {
  144. attrs[key] = (attr, message = "", opt = {}) => {
  145. const _attr =
  146. key === "range" ? { min: attr[0], max: attr[1] } : { [key]: attr };
  147. return {
  148. message: message.replace(
  149. `:${key}`,
  150. key === "range" ? `${attr[0]}-${attr[1]}` : attr
  151. ),
  152. type: "string",
  153. ..._attr,
  154. ...opt
  155. };
  156. };
  157. bindMessage(attrs[key], baseAttr[key]);
  158. return attrs;
  159. }, {});
  160. export default attrs;
  161. // var padDate = function (value) {
  162. // return value < 10 ? '0' + value : value;
  163. // };
  164. // export function formatDate (value, fmt) {
  165. // var date = new Date(value);
  166. // var year = date.getFullYear();
  167. // var month = padDate(date.getMonth() + 1);
  168. // var day = padDate(date.getDate());
  169. // var hours = padDate(date.getHours());
  170. // var minutes = padDate(date.getMinutes());
  171. // var seconds = padDate(date.getSeconds());
  172. // return year + '-' + month + '-' + day + '-' + ' ' + hours + ':' + minutes + ':' + seconds;
  173. // };