Skip to content

Commit

Permalink
validate id card
Browse files Browse the repository at this point in the history
  • Loading branch information
class100 committed May 7, 2021
1 parent 6a89349 commit ef15534
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions xvalidator/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func RegisterCustomValidator() {
v := binding.Validator.Engine().(*validator.Validate)
v.RegisterValidation("is-objectid", ValidateObjectId)
v.RegisterValidation("ValidateMobile", ValidateMobile)
v.RegisterValidation("ValidateIdCard", ValidateIdCard)
}

func ValidateObjectId(fl validator.FieldLevel) bool {
Expand All @@ -35,3 +36,17 @@ func ValidateMobile(fl validator.FieldLevel) bool {
}
return false
}

func ValidateIdCard(fl validator.FieldLevel) bool {
regularExp := "^(\\d{17})([0-9]|X|x)$"
regExp := regexp.MustCompile(regularExp)
mobile, ok := fl.Field().Interface().(string)
if !ok {
return false
}

if regExp.MatchString(mobile) {
return true
}
return false
}

0 comments on commit ef15534

Please sign in to comment.