verify.js 1.5 KB

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