forked from dingoblog/dingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
52 lines (41 loc) · 1.05 KB
/
validator.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package utils
import (
"regexp"
"strings"
)
var (
regexEmail *regexp.Regexp
regexASCII *regexp.Regexp
regexEnglish *regexp.Regexp
regexURL *regexp.Regexp
)
func init() {
regexEmail, _ = regexp.Compile(`(?i)[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}`)
regexASCII, _ = regexp.Compile(`^[a-zA-Z0-9-]+$`)
regexEnglish, _ = regexp.Compile(`^[a-zA-Z]+$`)
regexURL, _ = regexp.Compile(`(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?`)
}
func IsEmptyString(str string) bool {
return len(str) == 0
}
func IsEmail(str string) bool {
return regexEmail.MatchString(str)
}
func IsURL(str string) bool {
return regexURL.MatchString(str)
}
func IsLonger(str string, length int) bool {
return len(str) > length
}
func IsShorter(str string, length int) bool {
return len(str) < length
}
func IsASCII(str string) bool {
return regexASCII.MatchString(str)
}
func IsEnglish(str string) bool {
return regexEnglish.MatchString(str)
}
func IsContain(str string, contain string) bool {
return strings.Contains(str, contain)
}