-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_phone_loose.go
45 lines (37 loc) · 1.09 KB
/
builtin_phone_loose.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package builtin
import (
"errors"
"github.com/gogf/gf/v2/text/gregex"
)
// RulePhoneLoose implements `phone-loose` rule:
// Loose mobile phone number verification(宽松的手机号验证)
// As long as the 11 digits numbers beginning with
// 13, 14, 15, 16, 17, 18, 19 can pass the verification
// (只要满足 13、14、15、16、17、18、19开头的11位数字都可以通过验证).
//
// Format: phone-loose
type RulePhoneLoose struct{}
func init() {
Register(RulePhoneLoose{})
}
func (r RulePhoneLoose) Name() string {
return "phone-loose"
}
func (r RulePhoneLoose) Message() string {
return "The {field} value `{value}` is not a valid phone number"
}
func (r RulePhoneLoose) Run(in RunInput) error {
ok := gregex.IsMatchString(
`^1(3|4|5|6|7|8|9)\d{9}$`,
in.Value.String(),
)
if ok {
return nil
}
return errors.New(in.Message)
}