valiargs.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /** 针对后端验证 统一前后端封装为一致验证结果 */
  2. const msgMap = {
  3. isNotNull: "不能为空",
  4. isInteger: "只能为纯数字",
  5. isDecimal: "小数浮点格式不正确",
  6. isPrimes: "不是质数",
  7. isLetter: "只能纯字母",
  8. isUpperCase: "只能为大写字母",
  9. isLowerCase: "只能为小写字母",
  10. isIp: "IP地址不正确",
  11. isIpv4: "IPV4地址不正确",
  12. isIpv6: "IPV6地址不正确",
  13. isMoney: "金额格式不正确",
  14. isEmail: "邮箱格式不正确",
  15. isMobile: "手机号格式不正确",
  16. isCitizenid: "身份证格式不正确",
  17. isChinese: "只能汉字",
  18. isGeneral: "只能为字母、数字或下划线",
  19. isGeneralWithChinese: "只能为汉字、字母、数字和下划线",
  20. isZipCode: "邮编地址不正确",
  21. isUrl: "URL地址不正确",
  22. isMac: "MAC地址不正确",
  23. isPlateNumber: "中国车牌号不正确",
  24. isSecurityPassword: "密码至少包含大小写字母、数字、特殊字符,且不少于6位",
  25. };
  26. /**
  27. * @description 判断是否为空
  28. * @returns {boolean}
  29. * @param val
  30. */
  31. export function isNull(val) {
  32. /**
  33. * 判断是否为空
  34. * val
  35. * @returns
  36. */
  37. return (
  38. val === undefined ||
  39. val == null ||
  40. val === "" ||
  41. val === "" ||
  42. val === "undefined" ||
  43. val === "null" ||
  44. val === "NULL"
  45. );
  46. }
  47. /**
  48. * @description 不能为空
  49. * @returns {boolean}
  50. * @param val
  51. */
  52. export function isNotNull(val) {
  53. /**
  54. * 判断是否为空
  55. * val
  56. * @returns
  57. */
  58. return !(
  59. val === undefined ||
  60. val == null ||
  61. val === "" ||
  62. val === "" ||
  63. val === "undefined" ||
  64. val === "null" ||
  65. val === "NULL"
  66. );
  67. }
  68. /**
  69. * @description 只能为字母、数字或下划线
  70. * @param str
  71. * @returns {boolean}
  72. */
  73. export function isGeneral(str) {
  74. str = str + "";
  75. const reg = /^\w+$/;
  76. return reg.test(str);
  77. }
  78. /**
  79. * @description 判断是否为整数
  80. * @param str
  81. * @returns {boolean}
  82. */
  83. export function isInteger(str) {
  84. str = str + "";
  85. const reg = /^-?[1-9]\d*$/;
  86. return reg.test(str);
  87. }
  88. /**
  89. * @description 判断是否是小数浮点
  90. * @param str
  91. * @returns {boolean}
  92. */
  93. export function isDecimal(str) {
  94. str = str + "";
  95. const reg = /^(-?\d+)(\.\d+)?$/;
  96. return reg.test(str);
  97. }
  98. /**
  99. * @description 判断是否是质数
  100. * @param str
  101. * @returns {boolean}
  102. */
  103. export function isPrimes(str) {
  104. if (!isInteger(str)) {
  105. return false;
  106. }
  107. const n = parseInt(str);
  108. for (let i = 2; i <= Math.sqrt(n); i++) {
  109. if (n % i === 0) {
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115. /**
  116. * @description 判断是否为纯字母
  117. * @param str
  118. * @returns {boolean}
  119. */
  120. export function isLetter(str) {
  121. str = str + "";
  122. const reg = /^[a-zA-Z]+$/;
  123. return reg.test(str);
  124. }
  125. /**
  126. * @description 判断是否是大写字母
  127. * @param str
  128. * @returns {boolean}
  129. */
  130. export function isUpperCase(str) {
  131. str = str + "";
  132. const reg = /^[A-Z]+$/;
  133. return reg.test(str);
  134. }
  135. /**
  136. * @description 判断是否是小写字母
  137. * @param str
  138. * @returns {boolean}
  139. */
  140. export function isLowerCase(str) {
  141. str = str + "";
  142. const reg = /^[a-z]+$/;
  143. return reg.test(str);
  144. }
  145. /**
  146. * @description 判断是否是Ipv4
  147. * @param str
  148. * @returns {boolean}
  149. */
  150. export function isIpv4(str) {
  151. str = str + "";
  152. const reg =
  153. /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
  154. return reg.test(str);
  155. }
  156. /**
  157. * @description 判断是否是Ipv6
  158. * @param str
  159. * @returns {boolean}
  160. */
  161. export function isIpv6(str) {
  162. str = str + "";
  163. const reg =
  164. /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))$/;
  165. return reg.test(str);
  166. }
  167. /**
  168. * @description 判断是否是Ip
  169. * @param str
  170. * @returns {boolean}
  171. */
  172. export function isIp(str) {
  173. return isIpv4(str) || isIp(str);
  174. }
  175. /**
  176. * @description 判断是否是金额
  177. * @param str
  178. * @returns {boolean}
  179. */
  180. export function isMoney(str) {
  181. str = str + "";
  182. const reg = /^(\d+(?:\.\d+)?)$/;
  183. return reg.test(str);
  184. }
  185. /**
  186. * @description 判断是否是邮箱
  187. * @param str
  188. * @returns {boolean}
  189. */
  190. export function isEmail(str) {
  191. str = str + "";
  192. const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  193. return reg.test(str);
  194. }
  195. /**
  196. * @description 判断是否是手机号
  197. * @param str
  198. * @returns {boolean}
  199. */
  200. export function isMobile(str) {
  201. str = str + "";
  202. const reg = /^1\d{10}$/;
  203. return reg.test(str);
  204. }
  205. /**
  206. * @description 判断是否是18位身份证
  207. * @param str
  208. * @returns {boolean}
  209. */
  210. export function isCitizenid(str) {
  211. str = str + "";
  212. const reg =
  213. /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
  214. return reg.test(str);
  215. }
  216. /**
  217. * @description 判断是否是 URL
  218. * @param str
  219. * @returns {boolean}
  220. */
  221. export function isUrl(str) {
  222. str = str + "";
  223. const reg =
  224. /^(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.,?'\\+&%$#=~_-]+))*$/;
  225. return reg.test(str);
  226. }
  227. /**
  228. * @description 判断是否中文
  229. * @param str
  230. * @returns {boolean}
  231. */
  232. export function isChinese(str) {
  233. str = str + "";
  234. const reg = /^[\u4E00-\u9FA5]+$/;
  235. return reg.test(str);
  236. }
  237. /**
  238. * @description 判断是汉字,字母,数字和下划线
  239. * @param str
  240. * @returns {boolean}
  241. */
  242. export function isGeneralWithChinese(str) {
  243. str = str + "";
  244. const reg = /^[\u4E00-\u9FA5a-zA-Z0-9_]+$/;
  245. return reg.test(str);
  246. }
  247. /**
  248. * @description 判断是MAC地址
  249. * @param str
  250. * @returns {boolean}
  251. */
  252. export function isMac(str) {
  253. str = str + "";
  254. const reg =
  255. /[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}:[A-F\d]{2}/;
  256. return reg.test(str);
  257. }
  258. /**
  259. * @description 判断是是否是邮编
  260. * @param str
  261. * @returns {boolean}
  262. */
  263. export function isZipCode(str) {
  264. str = str + "";
  265. const reg =
  266. /^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[0-7]|6[0-7]|7[0-5]|8[0-9]|9[0-8])\d{4}|99907[78]$/;
  267. return reg.test(str);
  268. }
  269. /**
  270. * @description 判断是中国车牌
  271. * @param str
  272. * @returns {boolean}
  273. */
  274. export function isPlateNumber(str) {
  275. str = str + "";
  276. const reg =
  277. /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[ABCDEFGHJK])|([ABCDEFGHJK]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]\d{3}\d{1,3}[领])|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/;
  278. return reg.test(str);
  279. }
  280. /**
  281. * @description 密码至少包含大小写字母、数字、特殊字符,且不少于6位
  282. * @param str
  283. * @returns {boolean}
  284. */
  285. export function isSecurityPassword(str) {
  286. const reg =
  287. /^\S*(?=\S{6,})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*?.])\S*$/;
  288. return reg.test(str);
  289. }
  290. /**
  291. * 获得Msg
  292. * @returns {boolean}
  293. * @param key
  294. */
  295. export function getMsg(key) {
  296. const msg = msgMap[key];
  297. return isNotNull(msg) ? msg : "未知异常信息";
  298. }