validate.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * validate email
  3. * @param email
  4. * @returns {boolean}
  5. */
  6. export function validateEmail(email) {
  7. const re = /^(([^<>()\\[\]\\.,;:\s@"]+(\.[^<>()\\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  8. return re.test(email);
  9. }
  10. /**
  11. * 判断手机号码是否正确
  12. */
  13. export function validateMobile(phone) {
  14. const re = /^1(3|4|5|7|8|9|6)\d{9}$/
  15. return re.test(phone);
  16. }
  17. /**
  18. * 判断是否为空
  19. */
  20. export function validatenull(val) {
  21. if (typeof val === 'boolean') {
  22. return false;
  23. }
  24. if (typeof val === 'number') {
  25. return false;
  26. }
  27. if (val instanceof Array) {
  28. if (val.length == 0) return true;
  29. } else if (val instanceof Object) {
  30. if (JSON.stringify(val) === '{}') return true;
  31. } else {
  32. if (val == 'null' || val == null || val == 'undefined' || val == undefined || val == '') return true;
  33. return false;
  34. }
  35. return false;
  36. }
  37. module.exports = {
  38. validatenull: validatenull,
  39. validateEmail: validateEmail,
  40. validateMobile: validateMobile
  41. };