validate.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Created by PanJiaChen on 16/11/18.
  3. */
  4. import { DictData } from "@/api/basedata/home"
  5. import { ResearchReport } from "@/api/report/report"
  6. import { ar } from "date-fns/locale"
  7. // 身份证校验
  8. export function isCardNo (card: string) {
  9. const reg = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/
  10. return reg.test(card)
  11. }
  12. // 校验密码强度
  13. export function passwordLevel (value: string) {
  14. let modes = 0
  15. let level = 0
  16. // 未输入
  17. if (value.length < 1) {
  18. level = 0
  19. } else if (value.length >= 1 && value.length < 8) {
  20. // 输入大于0小于8个字符
  21. modes = 1
  22. } else {
  23. if (/\d/.test(value)) modes++ // 数字
  24. if (/[a-z]/.test(value)) modes++ // 小写
  25. if (/[A-Z]/.test(value)) modes++ // 大写
  26. if (/\W/.test(value)) modes++ // 特殊字符
  27. }
  28. switch (modes) {
  29. case 1:
  30. level = 1
  31. break
  32. case 2:
  33. level = 2
  34. break
  35. case 3:
  36. case 4:
  37. level = 3
  38. break
  39. default:
  40. level = 0
  41. break
  42. }
  43. return level
  44. }
  45. /**
  46. * 手机号码校验
  47. */
  48. export function isPhoneNumber (code: string) {
  49. let result = false
  50. // 手机
  51. const mobile = /^0?(13|14|15|16|17|18|19)[0-9]{9}$/
  52. // 固定电话
  53. const tel = /^[0-9]{3,4}[-][0-9]{8}$/
  54. const mbState = mobile.test(code)
  55. const telState = tel.test(code)
  56. if (mbState || telState) {
  57. result = true
  58. }
  59. return result
  60. }
  61. /**
  62. *
  63. * @param code 邮箱校验
  64. * @returns
  65. */
  66. export function isEmail (code: string) {
  67. let result = false;
  68. const emailReg = /^([a-zA-Z\d][\w-]{2,})@(\w{2,})\.([a-z]{2,})(\.[a-z]{2,})?$/
  69. if (emailReg.test(code)) {
  70. result = true;
  71. }
  72. return result;
  73. }
  74. /**
  75. * @param {string} path
  76. * @returns {Boolean}
  77. */
  78. export function isExternal (path: string): boolean {
  79. return /^(https?:|mailto:|tel:)/.test(path)
  80. }
  81. /**
  82. * @param {string} str
  83. * @returns {Boolean}
  84. */
  85. export function validUsername (str: string): boolean {
  86. const validMap = ['admin', 'editor']
  87. return validMap.indexOf(str.trim()) >= 0
  88. }
  89. /**
  90. * @param {string} url
  91. * @returns {Boolean}
  92. */
  93. export function validURL (url: string): boolean {
  94. 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.,?'\\+&%$#=~_-]+))*$/
  95. return reg.test(url)
  96. }
  97. /**
  98. * @param {string} str
  99. * @returns {Boolean}
  100. */
  101. export function validLowerCase (str: string): boolean {
  102. const reg = /^[a-z]+$/
  103. return reg.test(str)
  104. }
  105. /**
  106. * @param {string} str
  107. * @returns {Boolean}
  108. */
  109. export function validUpperCase (str: string): boolean {
  110. const reg = /^[A-Z]+$/
  111. return reg.test(str)
  112. }
  113. /**
  114. * @param {string} str
  115. * @returns {Boolean}
  116. */
  117. export function validAlphabets (str: string): boolean {
  118. const reg = /^[A-Za-z]+$/
  119. return reg.test(str)
  120. }
  121. /**
  122. *
  123. * @returns { Boolean }
  124. */
  125. export function isMobile (): boolean {
  126. if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)) {
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * 包含项
  134. * @param arr
  135. * @param marketType
  136. * @returns item
  137. */
  138. export function containsType (arr: ResearchReport[], marketType: string) {
  139. return arr.find(item => item.marketType === marketType);
  140. }
  141. /**
  142. * 判断是否包含
  143. * @param arr 数组对象
  144. * @param marketType 值
  145. * @returns true or false
  146. */
  147. export function containsMarketType (arr: DictData[], marketType: string) {
  148. return arr.some(item => item.dictValue === marketType);
  149. }