validate.js 924 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /**
  2. * 邮箱
  3. * @param {*} s
  4. */
  5. export function isEmail (s) {
  6. return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)
  7. }
  8. /**
  9. * 手机号码
  10. * @param {*} s
  11. */
  12. export function isMobile (s) {
  13. return /^1[0-9]{10}$/.test(s)
  14. }
  15. /**
  16. * 电话号码
  17. * @param {*} s
  18. */
  19. export function isPhone (s) {
  20. return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s)
  21. }
  22. /**
  23. * URL地址
  24. * @param {*} s
  25. */
  26. export function isURL (s) {
  27. return /^http[s]?:\/\/.*/.test(s)
  28. }
  29. // 整数范围值校验
  30. export const intRangeValidator = (min, max) => (rule, value, callback) => {
  31. const isInRange = (value >= min) && (value <= max)
  32. const reg = /^-?\d+$/
  33. const isInt = reg.test(value)
  34. if (isInRange && isInt) {
  35. return callback()
  36. } else {
  37. // return callback(new Error(`要求是在${min}到${max}的整数 [${min}, ${max}]`))
  38. return callback(new Error('输入数字不符合要求,请检查!'))
  39. }
  40. }