validate.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // 判断类型集合
  2. export const checkStr = (str, type) => {
  3. switch (type) {
  4. case 'mobile': //手机号码
  5. return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
  6. case 'tel': //座机
  7. return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
  8. case 'card': //身份证
  9. return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
  10. case 'mobileCode': //6位数字验证码
  11. return /^[0-9]{4}$/.test(str)
  12. case 'pwd': //密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线
  13. return /^([a-zA-Z0-9_]){6,18}$/.test(str)
  14. case 'payPwd': //支付密码 6位纯数字
  15. return /^[0-9]{6}$/.test(str)
  16. case 'postal': //邮政编码
  17. return /[1-9]\d{5}(?!\d)/.test(str);
  18. case 'QQ': //QQ号
  19. return /^[1-9][0-9]{4,9}$/.test(str);
  20. case 'email': //邮箱
  21. return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
  22. case 'money': //金额(小数点2位)
  23. return /^\d*(?:\.\d{0,2})?$/.test(str);
  24. case 'URL': //网址
  25. return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
  26. case 'IP': //IP
  27. return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
  28. case 'date': //日期时间
  29. return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) || /^(\d{4})\-(\d{2})\-(\d{2})$/
  30. .test(str)
  31. case 'number': //数字
  32. return /^[0-9]$/.test(str);
  33. case 'english': //英文
  34. return /^[a-zA-Z]+$/.test(str);
  35. case 'chinese': //中文
  36. return /^[\\u4E00-\\u9FA5]+$/.test(str);
  37. case 'lower': //小写
  38. return /^[a-z]+$/.test(str);
  39. case 'upper': //大写
  40. return /^[A-Z]+$/.test(str);
  41. case 'HTML': //HTML标记
  42. return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
  43. default:
  44. return true;
  45. }
  46. }