-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathidcard.go
78 lines (68 loc) · 1.57 KB
/
idcard.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
package validator
import (
"strconv"
)
var provMap = map[int64]string{
11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古",
21: "辽宁", 22: "吉林", 23: "黑龙江",
31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东",
41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南",
50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏",
61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆",
71: "台湾", 81: "香港", 82: "澳门", 91: "国外",
}
func Parse(card string) (ok bool, province string, year, month, day int64) {
if len(card) != 15 && len(card) != 18 {
return
}
province_i, err := strconv.ParseInt(card[0:3], 10, 64)
if err != nil {
return
}
province, _ = provMap[province_i]
var month_s, day_s string
if len(card) == 15 {
year, err = strconv.ParseInt(card[6:8], 10, 64)
if err != nil {
return
}
year += 1900
month_s = card[8:10]
day_s = card[10:12]
} else {
year, err = strconv.ParseInt(card[6:10], 10, 64)
if err != nil {
return
}
month_s = card[10:12]
day_s = card[12:14]
}
if year < 1700 || year > 3000 {
return
}
month, err = strconv.ParseInt(month_s, 10, 64)
if err != nil {
return
}
if month > 12 {
return
}
day, err = strconv.ParseInt(day_s, 10, 64)
if err != nil {
return
}
if day > 31 {
return
}
ok = true
return
}
func idcardCheck(v interface{}, param string) error {
if id, ok := v.(string); ok {
ok, _, _, _, _ = Parse(id)
if ok {
return nil
}
}
return ErrIdCard
}