validate.ts 747 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //电话号码
  2. function isPhone(s: string): boolean {
  3. return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s);
  4. }
  5. // 手机号码
  6. function isM(s: string): boolean {
  7. return /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(s);
  8. }
  9. function valCode(code: string): string {
  10. let msg = "";
  11. if (code === '') {
  12. msg = "请输入验证码!"
  13. } else if (code.length < 6) {
  14. msg = "验证码长度为6位!"
  15. }
  16. return msg
  17. }
  18. function valMobile(mobile: string): string {
  19. let msg = "";
  20. if (mobile === '') {
  21. msg = "手机号不能为空!"
  22. } else if (mobile.length < 11) {
  23. msg = "手机号码长度应为11位!"
  24. } else if (!isM(mobile)) {
  25. msg = "请输入有效的手机号!"
  26. }
  27. return msg
  28. }
  29. export {
  30. isPhone,
  31. isM,
  32. valCode,
  33. valMobile
  34. }