-
Notifications
You must be signed in to change notification settings - Fork 6
/
regexp.go
64 lines (50 loc) · 1010 Bytes
/
regexp.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
53
54
55
56
57
58
59
60
61
62
63
64
package str
import (
"regexp"
"strings"
)
func IsMatch(val, pattern string) bool {
match, err := regexp.Match(pattern, []byte(val))
if err != nil {
return false
}
return match
}
func IsEnglishIdentifier(val string, pattern ...string) bool {
defpattern := "^[a-zA-Z0-9\\-\\_\\.]+$"
if len(pattern) > 0 {
defpattern = pattern[0]
}
return IsMatch(val, defpattern)
}
func IsMail(val string) bool {
return IsMatch(val, `\w[-._\w]*@\w[-._\w]*\.\w+`)
}
func IsPhone(val string) bool {
if strings.HasPrefix(val, "+") {
return IsMatch(val[1:], `\d{13}`)
} else {
return IsMatch(val, `\d{11}`)
}
}
func HasDangerousCharacters(str string) bool {
if strings.Contains(str, "<") {
return true
}
if strings.Contains(str, ">") {
return true
}
if strings.Contains(str, "&") {
return true
}
if strings.Contains(str, "'") {
return true
}
if strings.Contains(str, "\"") {
return true
}
return false
}
func Dangerous(str string) bool {
return HasDangerousCharacters(str)
}