validate.go 957 B

123456789101112131415161718192021222324252627282930313233343536
  1. package validate
  2. import (
  3. "regexp"
  4. "go-common/library/net/http/blademaster/binding"
  5. "gopkg.in/go-playground/validator.v9"
  6. )
  7. var (
  8. //RegIDcheck 检查身份证
  9. RegIDcheck = regexp.MustCompile(`(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X)$)`)
  10. //RegHTTPCheck 检查HTTP格式
  11. RegHTTPCheck = regexp.MustCompile(`^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+`)
  12. //RegPhoneCheck 检查电话格式
  13. RegPhoneCheck = regexp.MustCompile(`1[345678]\d{9}`)
  14. )
  15. func init() {
  16. binding.Validator.RegisterValidation("idcheck", idcheck)
  17. binding.Validator.RegisterValidation("httpcheck", httpcheck)
  18. binding.Validator.RegisterValidation("phonecheck", phonecheck)
  19. }
  20. func idcheck(fl validator.FieldLevel) bool {
  21. return RegIDcheck.MatchString(fl.Field().String())
  22. }
  23. func httpcheck(fl validator.FieldLevel) bool {
  24. return RegHTTPCheck.MatchString(fl.Field().String())
  25. }
  26. func phonecheck(fl validator.FieldLevel) bool {
  27. return RegPhoneCheck.MatchString(fl.Field().String())
  28. }