Skip to content

Commit

Permalink
Add Alphanumeric rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur White committed Jul 25, 2017
1 parent 1373d6f commit c96cb9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ var (
language.English: "It's not a letters-only string.",
language.French: "Ce n'est pas une suite de lettres (uniquement).",
}}
ErrNotAlphanumeric = &ErrorID{ID: "notAlphanumeric", Locales: map[language.Tag]string{
language.English: "It's not an alphanumeric-only string.",
language.French: "Ce n'est pas une suite alphanumérique (uniquement).",
}}
ErrNotEmail = &ErrorID{ID: "notEmail", Locales: map[language.Tag]string{
language.English: "It's not an email.",
language.French: "Ce n'est pas un e-mail.",
Expand Down
15 changes: 15 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ func Alpha(errs Errors, form *multipart.Form, key string) {
}
}

// Alphanumeric rule checks that value contains alphaumeric characters only.
func Alphanumeric(errs Errors, form *multipart.Form, key string) {
if form == nil && form.Value == nil {
return
}
for _, v := range form.Value[key] {
for i := 0; i < len(v); i++ {
if v[i] < 48 || v[i] > 57 && v[i] < 65 || v[i] > 90 && v[i] < 97 || v[i] > 122 {
errs.Add(key, &Error{Error: ErrNotAlphanumeric})
return
}
}
}
}

// Email rule checks that value represents an email.
func Email(errs Errors, form *multipart.Form, key string) {
if form == nil && form.Value == nil {
Expand Down

0 comments on commit c96cb9c

Please sign in to comment.