Skip to content

Commit

Permalink
update regular: IsIpv4Addr
Browse files Browse the repository at this point in the history
  • Loading branch information
wzyonggege committed Jun 15, 2020
1 parent 5f0f046 commit 3750c86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
21 changes: 16 additions & 5 deletions regular/regular.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import (
"regexp"
)

var emailRegexp = regexp.MustCompile(
"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$",
)
var (
emailRegexp = regexp.MustCompile(
"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$",
)

mobileRegexp = regexp.MustCompile(
`^(?:\+?86)?1(?:3\d{3}|5[^4\D]\d{2}|8\d{3}|7(?:[01356789]\d{2}|4(?:0\d|1[0-2]|9\d))|9[189]\d{2}|6[567]\d{2}|4[579]\d{2})\d{6}$`,
)

var mobileRegexp = regexp.MustCompile(
`^(?:\+?86)?1(?:3\d{3}|5[^4\D]\d{2}|8\d{3}|7(?:[01356789]\d{2}|4(?:0\d|1[0-2]|9\d))|9[189]\d{2}|6[567]\d{2}|4[579]\d{2})\d{6}$`,
ipv4Regexp = regexp.MustCompile(
`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`,
)
)

// IsEmail ...
Expand All @@ -25,6 +31,11 @@ func IsMobile(v string) bool {
return mobileRegexp.MatchString(v)
}

// IsIpv4Addr...
func IsIpv4Addr(v string) bool {
return ipv4Regexp.MatchString(v)
}

// IsBankNo ... from alipay
func IsBankNo(bankCard string) (b bool) {
type ValidateBankCard struct {
Expand Down
12 changes: 12 additions & 0 deletions regular/regular_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ func TestIsMobile(t *testing.T) {
})
}

func TestIsIpv4Addr(t *testing.T) {
Convey("Test is ipv4", t, func() {
So(IsIpv4Addr("11.11.11.1"), ShouldBeTrue)
So(IsIpv4Addr("1.1.1.1"), ShouldBeTrue)
So(IsIpv4Addr("255.255.255.0"), ShouldBeTrue)
So(IsIpv4Addr("255.255.255.255"), ShouldBeTrue)
So(IsIpv4Addr("255,255.255.255"), ShouldBeFalse)
So(IsIpv4Addr("255.255.255.256"), ShouldBeFalse)
So(IsIpv4Addr("1.1.1.1.1"), ShouldBeFalse)
})
}

func TestIsBankNo(t *testing.T) {
Convey("test bank no", t, func() {
t.Log(IsBankNo("6228481101100634315"))
Expand Down

0 comments on commit 3750c86

Please sign in to comment.