mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
Fix/339 (#344)
* feat: add ContainAny * fix: fix issue #339 --------- Co-authored-by: Jiawen <im@linjiawen.com>
This commit is contained in:
@@ -2309,7 +2309,7 @@ import "github.com/duke-git/lancet/v2/validator"
|
||||
[[play](https://go.dev/play/p/jlYApVLLGTZ)]
|
||||
- **<big>IsEmail</big>** : check if the string is a email address.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsEmail)]
|
||||
[[play](https://go.dev/play/p/Os9VaFlT33G)]
|
||||
[[play](https://go.dev/play/p/HVQ5LAe-vFz)]
|
||||
- **<big>IsEmptyString</big>** : check if the string is empty.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsEmptyString)]
|
||||
[[play](https://go.dev/play/p/dpzgUjFnBCX)]
|
||||
|
||||
@@ -2316,7 +2316,7 @@ import "github.com/duke-git/lancet/v2/validator"
|
||||
[[play](https://go.dev/play/p/jlYApVLLGTZ)]
|
||||
- **<big>IsEmail</big>** : 验证字符串是否是有效电子邮件地址。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsEmail)]
|
||||
[[play](https://go.dev/play/p/Os9VaFlT33G)]
|
||||
[[play](https://go.dev/play/p/HVQ5LAe-vFz)]
|
||||
- **<big>IsEmptyString</big>** : 验证字符串是否是空字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsEmptyString)]
|
||||
[[play](https://go.dev/play/p/dpzgUjFnBCX)]
|
||||
|
||||
@@ -549,7 +549,7 @@ func main() {
|
||||
func IsEmail(email string) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/Os9VaFlT33G)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/HVQ5LAe-vFz)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -559,13 +559,28 @@ import (
|
||||
|
||||
func main() {
|
||||
result1 := validator.IsEmail("abc@xyz.com")
|
||||
result2 := validator.IsEmail("a.b@@com")
|
||||
result2 := validator.IsEmail("user@domain.co")
|
||||
result3 := validator.IsEmail("test.user@example.org")
|
||||
result4 := validator.IsEmail("@abc@xyz.com")
|
||||
result5 := validator.IsEmail("a.b@@com")
|
||||
result6 := validator.IsEmail("a.b@com")
|
||||
result7 := validator.IsEmail("test@example")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
fmt.Println(result5)
|
||||
fmt.Println(result6)
|
||||
fmt.Println(result7)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
// false
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
@@ -551,7 +551,7 @@ func main() {
|
||||
func IsEmail(email string) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/Os9VaFlT33G)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/HVQ5LAe-vFz)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -561,13 +561,28 @@ import (
|
||||
|
||||
func main() {
|
||||
result1 := validator.IsEmail("abc@xyz.com")
|
||||
result2 := validator.IsEmail("a.b@@com")
|
||||
result2 := validator.IsEmail("user@domain.co")
|
||||
result3 := validator.IsEmail("test.user@example.org")
|
||||
result4 := validator.IsEmail("@abc@xyz.com")
|
||||
result5 := validator.IsEmail("a.b@@com")
|
||||
result6 := validator.IsEmail("a.b@com")
|
||||
result7 := validator.IsEmail("test@example")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
fmt.Println(result5)
|
||||
fmt.Println(result6)
|
||||
fmt.Println(result7)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
// false
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/mail"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
@@ -26,6 +25,7 @@ var (
|
||||
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||
// dnsMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
|
||||
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])?)$`)
|
||||
emailMatcher *regexp.Regexp = regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`)
|
||||
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}$`)
|
||||
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])`)
|
||||
chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]")
|
||||
@@ -321,10 +321,9 @@ func IsDns(dns string) bool {
|
||||
}
|
||||
|
||||
// IsEmail check if the string is a email address.
|
||||
// Play: https://go.dev/play/p/Os9VaFlT33G
|
||||
// Play: https://go.dev/play/p/HVQ5LAe-vFz
|
||||
func IsEmail(email string) bool {
|
||||
_, err := mail.ParseAddress(email)
|
||||
return err == nil
|
||||
return emailMatcher.MatchString(strings.ToLower(email))
|
||||
}
|
||||
|
||||
// IsChineseMobile check if the string is chinese mobile number.
|
||||
|
||||
@@ -494,8 +494,12 @@ func TestIsEmail(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestIsEmail")
|
||||
|
||||
assert.Equal(true, IsEmail("abc@xyz.com"))
|
||||
assert.Equal(true, IsEmail("user@domain.co"))
|
||||
assert.Equal(true, IsEmail("test.user@example.org"))
|
||||
assert.Equal(false, IsEmail("@abc@xyz.com"))
|
||||
assert.Equal(false, IsEmail("a.b@@com"))
|
||||
assert.Equal(false, IsEmail("a.b@com"))
|
||||
assert.Equal(false, IsEmail("test@example"))
|
||||
}
|
||||
|
||||
func TestContainChinese(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user