validators.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package utl
  2. import (
  3. "regexp"
  4. )
  5. var (
  6. patternEmail string = "^([.]?[a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$"
  7. expEmail *regexp.Regexp = regexp.MustCompile(patternEmail)
  8. patternPhone string = "^(13[0-9]|14[57]|15[0-35-9]|18[0-9]|17[0-9]|16[7]|19[1])\\d{8}$"
  9. expPhone *regexp.Regexp = regexp.MustCompile(patternPhone)
  10. patternAlpha string = "^[a-zA-Z]+[a-zA-Z0-9]*$"
  11. expAlpha *regexp.Regexp = regexp.MustCompile(patternAlpha)
  12. //patternSafePassword string = ""
  13. //expSafePassword *regexp.Regexp = regexp.MustCompile(patternSafePassword)
  14. )
  15. func IsEmail(email string) bool {
  16. return expEmail.MatchString(email)
  17. }
  18. func IsPhone(phone string) bool {
  19. return expPhone.MatchString(phone)
  20. }
  21. func IsPhoneSimple(phone string) bool {
  22. if len(phone) != 11 {
  23. return false
  24. }
  25. return phone[0] == 49
  26. }
  27. /** begin with alpha, composed with alpha and num */
  28. func IsAlpha(alpha string) bool {
  29. return expAlpha.MatchString(alpha)
  30. }
  31. func CheckPasswd(passwd string) (lower bool, num bool, upper bool, others bool) {
  32. lower, upper, num, others = false, false, false, false
  33. for _, ch := range passwd {
  34. if ch >= 0x41 && ch <= 0x5A { //A-Z
  35. upper = true
  36. } else if ch >= 0x61 && ch <= 0x7A { // a-z
  37. lower = true
  38. } else if ch >= 0x30 && ch <= 0x39 {
  39. num = true
  40. } else {
  41. others = true
  42. }
  43. }
  44. return
  45. }