-
Notifications
You must be signed in to change notification settings - Fork 1
/
regexp.go
84 lines (75 loc) · 1.8 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package function
import (
"regexp"
"strconv"
)
// IsChinese 是否全部为中文
func IsChinese(str string) bool {
return regexp.MustCompile("^[\u4e00-\u9fa5]+$").MatchString(str)
}
// CheckMobileNumRule 验证手机号码
func CheckMobileNumRule(mobile string) bool {
re := regexp.MustCompile(`^1[3456789]\d{9}$`)
return re.MatchString(mobile)
}
// CheckEmailRule 验证电子邮箱
func CheckEmailRule(email string) bool {
re := regexp.MustCompile(`^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$`)
return re.MatchString(email)
}
// CheckIdNumRuleSimple 简单校验身份证号码的位数+组成字符
func CheckIdNumRuleSimple(idNum string) bool {
re := regexp.MustCompile(`^((\d{18})|([0-9x]{18})|([0-9X]{18}))$`)
return re.MatchString(idNum)
}
// CheckIdNumRule 验证身份证号码的合法性
func CheckIdNumRule(idNum string) bool {
re := regexp.MustCompile(`^((\d{18})|([0-9x]{18})|([0-9X]{18}))$`)
if re.MatchString(idNum) == false {
return false
}
var (
idNumByte [18]byte
idNumByte17 [17]byte
)
for k, v := range []byte(idNum) {
idNumByte[k] = v
if k <= 16 {
idNumByte17[k] = v
}
}
return verifyId(checkId(idNumByte17), byte2int(idNumByte[17]))
}
func byte2int(x byte) byte {
if x == 88 {
return 'X'
}
return x - 48
}
func checkId(id [17]byte) int {
array := make([]int, 17)
for index, value := range id {
array[index], _ = strconv.Atoi(string(value))
}
var wi [17]int = [...]int{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}
var res int
for i := 0; i < 17; i++ {
res += array[i] * wi[i]
}
return res % 11
}
func verifyId(verify int, idByte byte) bool {
var temp byte
var i int
a18 := [11]byte{1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2}
for i = 0; i < 11; i++ {
if i == verify {
temp = a18[i]
break
}
}
if temp == idByte {
return true
}
return false
}