validate.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Created by PanJiaChen on 16/11/18.
  3. */
  4. /** https
  5. * @param {string} path
  6. * @returns {Boolean}
  7. */
  8. export function isExternal(path) {
  9. return /^(https?:|mailto:|tel:)/.test(path)
  10. }
  11. /** 用户名
  12. * @param {string} str
  13. * @returns {Boolean}
  14. */
  15. export function validUsername(str) {
  16. const valid_map = ['admin', 'editor']
  17. return valid_map.indexOf(str.trim()) >= 0
  18. }
  19. /** url
  20. * @param {string} url
  21. * @returns {Boolean}
  22. */
  23. export function validURL(url) {
  24. 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.,?'\\+&%$#=~_-]+))*$/
  25. return reg.test(url)
  26. }
  27. /** 小写字母
  28. * @param {string} str
  29. * @returns {Boolean}
  30. */
  31. export function validLowerCase(str) {
  32. const reg = /^[a-z]+$/
  33. return reg.test(str)
  34. }
  35. /** 大写字母
  36. * @param {string} str
  37. * @returns {Boolean}
  38. */
  39. export function validUpperCase(str) {
  40. const reg = /^[A-Z]+$/
  41. return reg.test(str)
  42. }
  43. /** 字母
  44. * @param {string} str
  45. * @returns {Boolean}
  46. */
  47. export function validAlphabets(str) {
  48. const reg = /^[A-Za-z]+$/
  49. return reg.test(str)
  50. }
  51. /** 邮箱
  52. * @param {string} email
  53. * @returns {Boolean}
  54. */
  55. export function validEmail(email) {
  56. 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,}))$/
  57. return reg.test(email)
  58. }
  59. /** 字符串
  60. * @param {string} str
  61. * @returns {Boolean}
  62. */
  63. export function isString(str) {
  64. if (typeof str === 'string' || str instanceof String) {
  65. return true
  66. }
  67. return false
  68. }
  69. /** 数组
  70. * @param {Array} arg
  71. * @returns {Boolean}
  72. */
  73. export function isArray(arg) {
  74. if (typeof Array.isArray === 'undefined') {
  75. return Object.prototype.toString.call(arg) === '[object Array]'
  76. }
  77. return Array.isArray(arg)
  78. }
  79. /**
  80. * 邮箱
  81. * @param {*} s
  82. */
  83. export function isEmail(s) {
  84. return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(
  85. s
  86. )
  87. }
  88. /**
  89. * 手机号码
  90. * @param {*} s
  91. */
  92. export function isMobile(s) {
  93. return /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(s)
  94. }
  95. /**
  96. * 电话号码
  97. * @param {*} s
  98. */
  99. export function isPhone(s) {
  100. return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
  101. }
  102. /**
  103. * 固定电话(支持分机)
  104. * @param {*} s
  105. */
  106. export function isExtension(s) {
  107. let type = true
  108. let arr = s.split('-')
  109. // console.log(arr);
  110. if (arr.length === 2 || arr.length === 3) {
  111. type = setF(arr)
  112. } else {
  113. type = false
  114. }
  115. // console.log('console' + type);
  116. return type
  117. ///0\d{2,3}-\d{7,8}(-\d{1,6})?/.test(s)
  118. }
  119. function setF(arr) {
  120. let type1 = true, type2 = true, type3 = true
  121. type1 = /0\d{2,3}?$/.test(arr[0])
  122. type2 = arr[1].length === 7 || arr[1].length === 8 ? /\d{7,8}$/.test(arr[1]) : false;
  123. if (arr.length === 3) {
  124. type3 = arr[2].length === 0 || arr[2].length > 6 ? false : /\d{1,6}$/.test(arr[2])
  125. }
  126. // console.log(type1, type2, type3);
  127. if (arr.length === 2) {
  128. return type1 && type2
  129. } else {
  130. return type1 && type2 && type3
  131. }
  132. }
  133. // (^(\d{11})$|^((\d{3}-)?\d{8}(-\d{1,4})?)$|^((\d{4}-)?\d{7}(-\d{1,4})?)$|^(\d{7,8})$)
  134. /**
  135. * 纳税人识别号
  136. * @param {*} s
  137. */
  138. export function isLicense(s) {
  139. return /[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/.test(s)
  140. }
  141. /**
  142. * 身份证号码
  143. * @param {*} s
  144. */
  145. export function isIDentityCard(s) {
  146. return /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
  147. s
  148. )
  149. }
  150. /**
  151. * URL地址
  152. * @param {*} s
  153. */
  154. export function isURL(s) {
  155. return /^http[s]?:\/\/.*/.test(s)
  156. }
  157. /**
  158. * 汉语
  159. * @param {*} s
  160. */
  161. export function isChinese(s) {
  162. return /[\u4e00-\u9fa5]$/.test(s)
  163. }
  164. /**
  165. * 数字
  166. * @param {*} s
  167. */
  168. export function isnumber(s) {
  169. return /^[0-9]*$/.test(s)
  170. }
  171. /**
  172. * 数字或者带小数点的数字
  173. * @param {*} s
  174. */
  175. export function isnumber2(s) {
  176. return /(^[0-9]{1,2}$)|(^[0-9]{1,2}[\.]{1}[0-9]{1,2}$)/.test(s)
  177. }
  178. /**
  179. * 数字或者带小数点的数字 1,2,3位小数
  180. * @param {*} s
  181. */
  182. export function isnumber3(s) {
  183. return /^(([^0][0-9]+|0)\.([0-9]{1,3})$)|^([^0][0-9]+|0)$/.test(s)
  184. }
  185. /**
  186. * 数字字母
  187. * @param {*} s
  188. */
  189. export function isAlphanumeric(s) {
  190. return /^[0-9A-Za-z]*$/.test(s)
  191. }
  192. /**
  193. * 表情包
  194. * @param {*} s
  195. */
  196. export function isEmoticon(s) {
  197. const reg = /[^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n]/g
  198. // console.log(reg.test(s));
  199. return reg.test(s)
  200. }
  201. /**
  202. * 判断是否为微信浏览器
  203. * @param {*} s
  204. */
  205. export function JudgeEnvironment() {
  206. const ua = navigator.userAgent.toLowerCase()
  207. let environment = ''
  208. const isWeixin = ua.indexOf('micromessenger') != -1
  209. if (isWeixin) {
  210. environment = 'Weixin'
  211. }
  212. const isWelink = ua.indexOf('huawei-anyoffice') != -1
  213. if (isWelink) {
  214. environment = 'Welink'
  215. }
  216. const isDingDing = ua.indexOf('dingtalk') != -1
  217. if (isDingDing) {
  218. environment = 'isDingDing'
  219. }
  220. if (environment === '') {
  221. environment = 'otherBrowser'
  222. }
  223. return environment
  224. }
  225. export function timestampToTime(timestamp) {
  226. const date = new Date(timestamp) // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
  227. const Y = date.getFullYear() + '-'
  228. const M =
  229. (date.getMonth() + 1 < 10
  230. ? '0' + (date.getMonth() + 1)
  231. : date.getMonth() + 1) + '-'
  232. const D = date.getDate() + ' '
  233. let h = date.getHours()
  234. h = h < 10 ? `0${h}:` : h + ':'
  235. let m = date.getMinutes()
  236. m = m < 10 ? `0${m}:` : m + ':'
  237. let s = date.getSeconds()
  238. s = s < 10 ? `0${s}` : s
  239. return Y + M + D + h + m + s
  240. }
  241. export function isCreditCode(s) {
  242. const reg = /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g;
  243. return reg.test(s)
  244. }