-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphanumeric.go
37 lines (29 loc) · 965 Bytes
/
alphanumeric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package checker
import (
"reflect"
"unicode"
)
// CheckerAlphanumeric is the name of the checker.
const CheckerAlphanumeric = "alphanumeric"
// ResultNotAlphanumeric indicates that the given string contains non-alphanumeric characters.
const ResultNotAlphanumeric = "NOT_ALPHANUMERIC"
// IsAlphanumeric checks if the given string consists of only alphanumeric characters.
func IsAlphanumeric(value string) Result {
for _, c := range value {
if !unicode.IsDigit(c) && !unicode.IsLetter(c) {
return ResultNotAlphanumeric
}
}
return ResultValid
}
// makeAlphanumeric makes a checker function for the alphanumeric checker.
func makeAlphanumeric(_ string) CheckFunc {
return checkAlphanumeric
}
// checkAlphanumeric checks if the given string consists of only alphanumeric characters.
func checkAlphanumeric(value, _ reflect.Value) Result {
if value.Kind() != reflect.String {
panic("string expected")
}
return IsAlphanumeric(value.String())
}