Skip to content

Commit 5c13fd4

Browse files
jiawen-afkJiawen
andauthored
Fix/339 (#344)
* feat: add ContainAny * fix: fix issue #339 --------- Co-authored-by: Jiawen <[email protected]>
1 parent 889d0cc commit 5c13fd4

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@ import "github.com/duke-git/lancet/v2/validator"
23092309
[[play](https://go.dev/play/p/jlYApVLLGTZ)]
23102310
- **<big>IsEmail</big>** : check if the string is a email address.
23112311
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsEmail)]
2312-
[[play](https://go.dev/play/p/Os9VaFlT33G)]
2312+
[[play](https://go.dev/play/p/HVQ5LAe-vFz)]
23132313
- **<big>IsEmptyString</big>** : check if the string is empty.
23142314
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsEmptyString)]
23152315
[[play](https://go.dev/play/p/dpzgUjFnBCX)]

README_zh-CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,7 @@ import "github.com/duke-git/lancet/v2/validator"
23162316
[[play](https://go.dev/play/p/jlYApVLLGTZ)]
23172317
- **<big>IsEmail</big>** : 验证字符串是否是有效电子邮件地址。
23182318
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsEmail)]
2319-
[[play](https://go.dev/play/p/Os9VaFlT33G)]
2319+
[[play](https://go.dev/play/p/HVQ5LAe-vFz)]
23202320
- **<big>IsEmptyString</big>** : 验证字符串是否是空字符串。
23212321
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsEmptyString)]
23222322
[[play](https://go.dev/play/p/dpzgUjFnBCX)]

docs/api/packages/validator.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ func main() {
549549
func IsEmail(email string) bool
550550
```
551551

552-
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/Os9VaFlT33G)</span></b>
552+
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/HVQ5LAe-vFz)</span></b>
553553

554554
```go
555555
import (
@@ -559,13 +559,28 @@ import (
559559

560560
func main() {
561561
result1 := validator.IsEmail("[email protected]")
562-
result2 := validator.IsEmail("a.b@@com")
562+
result2 := validator.IsEmail("[email protected]")
563+
result3 := validator.IsEmail("[email protected]")
564+
result4 := validator.IsEmail("@[email protected]")
565+
result5 := validator.IsEmail("a.b@@com")
566+
result6 := validator.IsEmail("a.b@com")
567+
result7 := validator.IsEmail("test@example")
563568

564569
fmt.Println(result1)
565570
fmt.Println(result2)
571+
fmt.Println(result3)
572+
fmt.Println(result4)
573+
fmt.Println(result5)
574+
fmt.Println(result6)
575+
fmt.Println(result7)
566576

567577
// Output:
568578
// true
579+
// true
580+
// true
581+
// false
582+
// false
583+
// false
569584
// false
570585
}
571586
```

docs/en/api/packages/validator.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func main() {
551551
func IsEmail(email string) bool
552552
```
553553

554-
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/Os9VaFlT33G)</span></b>
554+
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/HVQ5LAe-vFz)</span></b>
555555

556556
```go
557557
import (
@@ -561,13 +561,28 @@ import (
561561

562562
func main() {
563563
result1 := validator.IsEmail("[email protected]")
564-
result2 := validator.IsEmail("a.b@@com")
564+
result2 := validator.IsEmail("[email protected]")
565+
result3 := validator.IsEmail("[email protected]")
566+
result4 := validator.IsEmail("@[email protected]")
567+
result5 := validator.IsEmail("a.b@@com")
568+
result6 := validator.IsEmail("a.b@com")
569+
result7 := validator.IsEmail("test@example")
565570

566571
fmt.Println(result1)
567572
fmt.Println(result2)
573+
fmt.Println(result3)
574+
fmt.Println(result4)
575+
fmt.Println(result5)
576+
fmt.Println(result6)
577+
fmt.Println(result7)
568578

569579
// Output:
570580
// true
581+
// true
582+
// true
583+
// false
584+
// false
585+
// false
571586
// false
572587
}
573588
```

validator/validator.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"encoding/json"
99
"fmt"
1010
"net"
11-
"net/mail"
1211
"net/url"
1312
"reflect"
1413
"regexp"
@@ -26,6 +25,7 @@ var (
2625
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
2726
// dnsMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
2827
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*(?:xn--[a-zA-Z0-9\-]{1,59}|[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)$`)
28+
emailMatcher *regexp.Regexp = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`)
2929
chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`)
3030
chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`([1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx])|([1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx])`)
3131
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
@@ -321,10 +321,9 @@ func IsDns(dns string) bool {
321321
}
322322

323323
// IsEmail check if the string is a email address.
324-
// Play: https://go.dev/play/p/Os9VaFlT33G
324+
// Play: https://go.dev/play/p/HVQ5LAe-vFz
325325
func IsEmail(email string) bool {
326-
_, err := mail.ParseAddress(email)
327-
return err == nil
326+
return emailMatcher.MatchString(strings.ToLower(email))
328327
}
329328

330329
// IsChineseMobile check if the string is chinese mobile number.

validator/validator_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,12 @@ func TestIsEmail(t *testing.T) {
494494
assert := internal.NewAssert(t, "TestIsEmail")
495495

496496
assert.Equal(true, IsEmail("[email protected]"))
497+
assert.Equal(true, IsEmail("[email protected]"))
498+
assert.Equal(true, IsEmail("[email protected]"))
497499
assert.Equal(false, IsEmail("@[email protected]"))
498500
assert.Equal(false, IsEmail("a.b@@com"))
501+
assert.Equal(false, IsEmail("a.b@com"))
502+
assert.Equal(false, IsEmail("test@example"))
499503
}
500504

501505
func TestContainChinese(t *testing.T) {

0 commit comments

Comments
 (0)