verify.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * 用户输入内容验证类
  3. */
  4. // 是否为空
  5. export const isEmpty = (str) => {
  6. return !str || str.trim() == ''
  7. }
  8. /**
  9. * 匹配phone
  10. */
  11. export const isPhone = (str) => {
  12. const reg = /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/
  13. return reg.test(str)
  14. }
  15. /**
  16. * 匹配phone
  17. */
  18. export const isMobile = (str) => {
  19. const reg = /^(1[3456789]\d{9})$/
  20. return reg.test(str)
  21. }
  22. /**
  23. * 匹配Email地址
  24. */
  25. export const isEmail = (str) => {
  26. if (str == null || str == "") return false
  27. var result = str.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)
  28. if (result == null) return false
  29. return true
  30. }
  31. /**
  32. * 判断数值类型,包括整数和浮点数
  33. */
  34. export const isNumber = (str) => {
  35. if (isDouble(str) || isInteger(str)) return true
  36. return false
  37. }
  38. /**
  39. * 判断是否为正整数(只能输入数字[0-9])
  40. */
  41. export const isPositiveInteger = (str) => {
  42. return /(^[0-9]\d*$)/.test(str)
  43. }
  44. /**
  45. * 匹配integer
  46. */
  47. export const isInteger = (str) => {
  48. if (str == null || str == "") return false
  49. var result = str.match(/^[-\+]?\d+$/)
  50. if (result == null) return false
  51. return true
  52. }
  53. /**
  54. * 匹配double或float
  55. */
  56. export const isDouble = (str) => {
  57. if (str == null || str == "") return false
  58. var result = str.match(/^[-\+]?\d+(\.\d+)?$/)
  59. if (result == null) return false
  60. return true
  61. }