diff --git a/docs/validator.md b/docs/validator.md index db0c228..aea5faf 100644 --- a/docs/validator.md +++ b/docs/validator.md @@ -1,16 +1,17 @@ # Validator + Package validator contains some functions for data validation.
## Source: -- [https://github.com/duke-git/lancet/blob/main/validator/validator.go](https://github.com/duke-git/lancet/blob/main/validator/validator.go) - +- [https://github.com/duke-git/lancet/blob/main/validator/validator.go](https://github.com/duke-git/lancet/blob/main/validator/validator.go)
## Usage: + ```go import ( "github.com/duke-git/lancet/v2/validator" @@ -20,42 +21,42 @@ import (
## Index -- [ContainChinese](#ContainChinese) -- [ContainLetter](#ContainLetter) -- [ContainLower](#ContainLower) -- [ContainUpper](#ContainUpper) -- [IsAlpha](#IsAlpha) -- [IsAllUpper](#IsAllUpper) -- [IsAllLower](#IsAllLower) -- [IsBase64](#IsBase64) -- [IsChineseMobile](#IsChineseMobile) -- [IsChineseIdNum](#IsChineseIdNum) -- [IsChinesePhone](#IsChinesePhone) -- [IsCreditCard](#IsCreditCard) -- [IsDns](#IsDns) -- [IsEmail](#IsEmail) -- [IsEmptyString](#IsEmptyString) -- [IsFloatStr](#IsFloatStr) -- [IsNumberStr](#IsNumberStr) -- [IsJSON](#IsJSON) -- [IsRegexMatch](#IsRegexMatch) -- [IsIntStr](#IsIntStr) -- [IsIp](#IsIp) -- [IsIpV4](#IsIpV4) -- [IsIpV6](#IsIpV6) -- [IsStrongPassword](#IsStrongPassword) -- [IsUrl](#IsUrl) -- [IsWeakPassword](#IsWeakPassword) -- [IsZeroValue](#IsZeroValue) -- [IsGBK](#IsGBK) - + +- [ContainChinese](#ContainChinese) +- [ContainLetter](#ContainLetter) +- [ContainLower](#ContainLower) +- [ContainUpper](#ContainUpper) +- [IsAlpha](#IsAlpha) +- [IsAllUpper](#IsAllUpper) +- [IsAllLower](#IsAllLower) +- [IsBase64](#IsBase64) +- [IsChineseMobile](#IsChineseMobile) +- [IsChineseIdNum](#IsChineseIdNum) +- [IsChinesePhone](#IsChinesePhone) +- [IsCreditCard](#IsCreditCard) +- [IsDns](#IsDns) +- [IsEmail](#IsEmail) +- [IsEmptyString](#IsEmptyString) +- [IsFloatStr](#IsFloatStr) +- [IsNumberStr](#IsNumberStr) +- [IsJSON](#IsJSON) +- [IsRegexMatch](#IsRegexMatch) +- [IsIntStr](#IsIntStr) +- [IsIp](#IsIp) +- [IsIpV4](#IsIpV4) +- [IsIpV6](#IsIpV6) +- [IsStrongPassword](#IsStrongPassword) +- [IsUrl](#IsUrl) +- [IsWeakPassword](#IsWeakPassword) +- [IsZeroValue](#IsZeroValue) +- [IsGBK](#IsGBK)
## Documentation - ### ContainChinese +

Check if the string contain mandarin chinese.

Signature: @@ -63,29 +64,33 @@ import ( ```go func ContainChinese(s string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.ContainChinese("你好") - fmt.Println(res1) //true + result1 := validator.ContainChinese("你好") + result2 := validator.ContainChinese("你好hello") + result3 := validator.ContainChinese("hello") - res2 := validator.ContainChinese("你好hello") - fmt.Println(res2) //true + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.ContainChinese("hello") - fmt.Println(res3) //false + // Output: + // true + // true + // false } ``` - - ### ContainLetter +

Check if the string contain at least one letter.

Signature: @@ -93,30 +98,33 @@ func main() { ```go func ContainLetter(str string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.ContainLetter("1bc") - fmt.Println(res1) //true + result1 := validator.ContainLetter("你好") + result2 := validator.ContainLetter("&@#$%^&*") + result3 := validator.ContainLetter("ab1") - res2 := validator.ContainLetter("123") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.ContainLetter("&@#$%^&*") - fmt.Println(res3) //false + // Output: + // false + // false + // true } ``` - - - ### ContainLower +

Check if the string contain at least one lower case letter a-z.

Signature: @@ -124,30 +132,33 @@ func main() { ```go func ContainLower(str string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.ContainLower("1bc") - fmt.Println(res1) //true + result1 := validator.ContainLower("abc") + result2 := validator.ContainLower("aBC") + result3 := validator.ContainLower("ABC") - res2 := validator.ContainLower("123") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.ContainLower("1BC") - fmt.Println(res3) //false + // Output: + // true + // true + // false } ``` - - - ### ContainUpper +

Check if the string contain at least one upper case letter A-Z.

Signature: @@ -155,30 +166,33 @@ func main() { ```go func ContainUpper(str string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.ContainUpper("1bc") - fmt.Println(res1) //false + result1 := validator.ContainUpper("ABC") + result2 := validator.ContainUpper("abC") + result3 := validator.ContainUpper("abc") - res2 := validator.ContainUpper("123") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.ContainUpper("1BC") - fmt.Println(res3) //true + // Output: + // true + // true + // false } ``` - - - ### IsAlpha +

Check if the string contains only letters (a-zA-Z).

Signature: @@ -186,27 +200,33 @@ func main() { ```go func IsAlpha(s string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsAlpha("abc") - fmt.Println(res1) //true + result1 := validator.IsAlpha("abc") + result2 := validator.IsAlpha("ab1") + result3 := validator.IsAlpha("") - res2 := validator.IsAlpha("1bc") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.IsAlpha("") - fmt.Println(res3) //false + // Output: + // true + // false + // false } ``` ### IsAllUpper +

Check if string is all upper case letters A-Z.

Signature: @@ -214,27 +234,33 @@ func main() { ```go func IsAllUpper(str string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsAllUpper("ABC") - fmt.Println(res1) //true + result1 := validator.IsAllUpper("ABC") + result2 := validator.IsAllUpper("ABc") + result3 := validator.IsAllUpper("AB1") - res2 := validator.IsAllUpper("aBC") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false } ``` - - - ### IsAllLower +

Check if string is all lower case letters a-z.

Signature: @@ -242,27 +268,33 @@ func main() { ```go func IsAllLower(str string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsAllLower("abc") - fmt.Println(res1) //true + result1 := validator.IsAllLower("abc") + result2 := validator.IsAllLower("abC") + result3 := validator.IsAllLower("ab1") - res2 := validator.IsAllLower("abC") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false } ``` - - - ### IsBase64 +

Check if the string is base64 string.

Signature: @@ -270,27 +302,30 @@ func main() { ```go func IsBase64(base64 string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsBase64("aGVsbG8=") - fmt.Println(res1) //true + result1 := validator.IsBase64("aGVsbG8=") + result2 := validator.IsBase64("123456") - res2 := validator.IsBase64("123456") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsChineseMobile +

Check if the string is valid chinese mobile number.

Signature: @@ -298,26 +333,30 @@ func main() { ```go func IsChineseMobile(mobileNum string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsChineseMobile("13263527980") - fmt.Println(res1) //true + result1 := validator.IsChineseMobile("13263527980") + result2 := validator.IsChineseMobile("434324324") - res2 := validator.IsChineseMobile("434324324") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - ### IsChineseIdNum +

Check if the string is chinese id number.

Signature: @@ -325,27 +364,30 @@ func main() { ```go func IsChineseIdNum(id string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsChineseIdNum("210911192105130715") - fmt.Println(res1) //true + result1 := validator.IsChineseIdNum("210911192105130715") + result2 := validator.IsChineseIdNum("123456") - res2 := validator.IsChineseIdNum("123456") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsChinesePhone +

Check if the string is chinese phone number.

Signature: @@ -353,27 +395,30 @@ func main() { ```go func IsChinesePhone(phone string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsChinesePhone("010-32116675") - fmt.Println(res1) //true + result1 := validator.IsChinesePhone("010-32116675") + result2 := validator.IsChinesePhone("123-87562") - res2 := validator.IsChinesePhone("123-87562") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsCreditCard +

Check if the string is credit card.

Signature: @@ -381,27 +426,30 @@ func main() { ```go func IsCreditCard(creditCart string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsCreditCard("4111111111111111") - fmt.Println(res1) //true + result1 := validator.IsCreditCard("4111111111111111") + result2 := validator.IsCreditCard("123456") - res2 := validator.IsCreditCard("123456") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsDns +

Check if the string is valid dns.

Signature: @@ -409,30 +457,33 @@ func main() { ```go func IsDns(dns string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsDns("abc.com") - fmt.Println(res1) //true + result1 := validator.IsDns("abc.com") + result2 := validator.IsDns("a.b.com") + result3 := validator.IsDns("http://abc.com") - res2 := validator.IsDns("a.b.com") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.IsDns("http://abc.com") - fmt.Println(res3) //false + // Output: + // true + // false + // false } ``` - - - ### IsEmail +

Check if the string is email address.

Signature: @@ -440,28 +491,30 @@ func main() { ```go func IsEmail(email string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsEmail("abc@xyz.com") - fmt.Println(res1) //true + result1 := validator.IsEmail("abc@xyz.com") + result2 := validator.IsEmail("a.b@@com") - res2 := validator.IsEmail("a.b@@com") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - - ### IsEmptyString +

Check if the string is empty or not.

Signature: @@ -469,27 +522,33 @@ func main() { ```go func IsEmptyString(s string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsEmptyString("") - fmt.Println(res1) //true + result1 := validator.IsEmptyString("") + result2 := validator.IsEmptyString(" ") + result3 := validator.IsEmptyString("\t") - res2 := validator.IsEmptyString("abc") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false } ``` - - - ### IsFloatStr +

Check if the string can convert to a float.

Signature: @@ -497,28 +556,36 @@ func main() { ```go func IsFloatStr(s string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsFloatStr("")) //false - fmt.Println(validator.IsFloatStr("12a")) //false - fmt.Println(validator.IsFloatStr("3.")) //true - fmt.Println(validator.IsFloatStr("+3.")) //true - fmt.Println(validator.IsFloatStr("-3.")) //true - fmt.Println(validator.IsFloatStr("12")) //true + result1 := validator.IsFloatStr("3.") + result2 := validator.IsFloatStr("+3.") + result3 := validator.IsFloatStr("12") + result4 := validator.IsFloatStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false } ``` - - - ### IsNumberStr +

Check if the string can convert to a number.

Signature: @@ -526,28 +593,36 @@ func main() { ```go func IsNumberStr(s string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsNumberStr("")) //false - fmt.Println(validator.IsNumberStr("12a")) //false - fmt.Println(validator.IsNumberStr("3.")) //true - fmt.Println(validator.IsNumberStr("+3.")) //true - fmt.Println(validator.IsNumberStr("-3.")) //true - fmt.Println(validator.IsNumberStr("+3e2")) //true + result1 := validator.IsNumberStr("3.") + result2 := validator.IsNumberStr("+3.") + result3 := validator.IsNumberStr("+3e2") + result4 := validator.IsNumberStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false } ``` - - - ### IsJSON +

Check if the string is valid JSON.

Signature: @@ -555,28 +630,36 @@ func main() { ```go func IsJSON(str string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsJSON("")) //false - fmt.Println(validator.IsJSON("abc")) //false - fmt.Println(validator.IsJSON("{}")) //true - fmt.Println(validator.IsJSON("[]")) //true - fmt.Println(validator.IsJSON("123")) //true - fmt.Println(validator.IsJSON("{\"name\": \"test\"}")) //true + result1 := validator.IsJSON("{}") + result2 := validator.IsJSON("{\"name\": \"test\"}") + result3 := validator.IsIntStr("") + result4 := validator.IsIntStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false } ``` - - - ### IsRegexMatch +

Check if the string match the regexp.

Signature: @@ -584,25 +667,30 @@ func main() { ```go func IsRegexMatch(s, regex string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsRegexMatch("abc", `^[a-zA-Z]+$`)) //true - fmt.Println(validator.IsRegexMatch("1ab", `^[a-zA-Z]+$`)) //false - fmt.Println(validator.IsRegexMatch("", `^[a-zA-Z]+$`)) //false + result1 := validator.IsRegexMatch("abc", `^[a-zA-Z]+$`) + result2 := validator.IsRegexMatch("ab1", `^[a-zA-Z]+$`) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsIntStr +

Check if the string can convert to a integer.

Signature: @@ -610,26 +698,36 @@ func main() { ```go func IsIntStr(s string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsIntStr("+3")) //true - fmt.Println(validator.IsIntStr("-3")) //true - fmt.Println(validator.IsIntStr("3.")) //false - fmt.Println(validator.IsIntStr("abc")) //false + result1 := validator.IsIntStr("+3") + result2 := validator.IsIntStr("-3") + result3 := validator.IsIntStr("3.") + result4 := validator.IsIntStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false } ``` - - - ### IsIp +

Check if the string is a ip address.

Signature: @@ -637,26 +735,36 @@ func main() { ```go func IsIp(ipstr string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsIp("127.0.0.1")) //true - fmt.Println(validator.IsIp("::0:0:0:0:0:0:1")) //true - fmt.Println(validator.IsIp("127.0.0")) //false - fmt.Println(validator.IsIp("127")) //false + result1 := validator.IsIp("127.0.0.1") + result2 := validator.IsIp("::0:0:0:0:0:0:1") + result3 := validator.IsIp("127.0.0") + result4 := validator.IsIp("::0:0:0:0:") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false } ``` - - - ### IsIpV4 +

Check if the string is a ipv4 address.

Signature: @@ -664,26 +772,30 @@ func main() { ```go func IsIpV4(ipstr string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsIpV4("127.0.0.1")) //true - fmt.Println(validator.IsIpV4("::0:0:0:0:0:0:1")) //false - fmt.Println(validator.IsIpV4("127.0.0")) //false - fmt.Println(validator.IsIpV4("127")) //false + result1 := validator.IsIpV4("127.0.0.1") + result2 := validator.IsIpV4("::0:0:0:0:0:0:1") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsIpV6 +

Check if the string is a ipv6 address.

Signature: @@ -691,26 +803,30 @@ func main() { ```go func IsIpV6(ipstr string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsIpV6("127.0.0.1")) //false - fmt.Println(validator.IsIpV6("::0:0:0:0:0:0:1")) //true - fmt.Println(validator.IsIpV6("127.0.0")) //false - fmt.Println(validator.IsIpV6("127")) //false + result1 := validator.IsIpV6("127.0.0.1") + result2 := validator.IsIpV6("::0:0:0:0:0:0:1") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true } ``` - - - ### IsStrongPassword +

Check if the string is strong password (alpha(lower+upper) + number + special chars(!@#$%^&*()?><)).

Signature: @@ -718,54 +834,64 @@ func main() { ```go func IsStrongPassword(password string, length int) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsStrongPassword("abc", 3)) //false - fmt.Println(validator.IsStrongPassword("abc123", 6)) //false - fmt.Println(validator.IsStrongPassword("abcABC", 6)) //false - fmt.Println(validator.IsStrongPassword("abcABC123@#$", 16)) //false - fmt.Println(validator.IsStrongPassword("abcABC123@#$", 12)) //true + result1 := validator.IsStrongPassword("abcABC", 6) + result2 := validator.IsStrongPassword("abcABC123@#$", 10) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true } ``` - - - ### IsUrl +

Check if the string is url.

Signature: ```go -func IsUrl(str string) bool +func IsUrl(str string) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsUrl("http://abc.com")) //true - fmt.Println(validator.IsUrl("abc.com")) //true - fmt.Println(validator.IsUrl("a.b.com")) //true - fmt.Println(validator.IsUrl("abc")) //false + result1 := validator.IsUrl("abc.com") + result2 := validator.IsUrl("http://abc.com") + result3 := validator.IsUrl("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false } ``` - - - ### IsWeakPassword +

Checks if the string is weak password(only letter or only number or letter + number) .

@@ -774,26 +900,30 @@ func main() { ```go func IsWeakPassword(password string, length int) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsWeakPassword("abc")) //true - fmt.Println(validator.IsWeakPassword("123")) //true - fmt.Println(validator.IsWeakPassword("abc123")) //true - fmt.Println(validator.IsWeakPassword("abc123@#$")) //false + result1 := validator.IsWeakPassword("abcABC") + result2 := validator.IsWeakPassword("abc123@#$") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsZeroValue +

Checks if passed value is a zero value.

Signature: @@ -801,28 +931,36 @@ func main() { ```go func IsZeroValue(value any) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsZeroValue(nil)) //true - fmt.Println(validator.IsZeroValue(0)) //true - fmt.Println(validator.IsZeroValue("")) //true - fmt.Println(validator.IsZeroValue([]int)) //true - fmt.Println(validator.IsZeroValue(interface{})) //true + result1 := validator.IsZeroValue("") + result2 := validator.IsZeroValue(0) + result3 := validator.IsZeroValue("abc") + result4 := validator.IsZeroValue(1) - fmt.Println(validator.IsZeroValue("0")) //false - fmt.Println(validator.IsZeroValue("nil")) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false } ``` - ### IsGBK +

Checks if data encoding is gbk(Chinese character internal code extension specification). this function is implemented by whether double bytes fall within the encoding range of gbk,while each byte of utf-8 encoding format falls within the encoding range of gbk.Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding and then call IsGBK() to check gbk encoding. like the example.

Signature: @@ -830,28 +968,25 @@ func main() { ```go func IsGBK(data []byte) bool ``` + Example: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "golang.org/x/text/encoding/simplifiedchinese" + "github.com/duke-git/lancet/v2/validator" ) func main() { - data := []byte("你好") - - // check utf8 first - if utf8.Valid(data) { - fmt.Println("data encoding is utf-8") - }else if(validator.IsGBK(data)) { - fmt.Println("data encoding is GBK") - } - fmt.Println("data encoding is unknown") + str := "你好" + gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str)) + + result := validator.IsGBK(gbkData) + + fmt.Println(result) + + // Output: + // true } ``` - - - - - diff --git a/docs/validator_zh-CN.md b/docs/validator_zh-CN.md index 4de7a05..3515d5b 100644 --- a/docs/validator_zh-CN.md +++ b/docs/validator_zh-CN.md @@ -1,16 +1,17 @@ # Validator -validator验证器包,包含常用字符串格式验证函数。 + +validator 验证器包,包含常用字符串格式验证函数。
## 源码: -- [https://github.com/duke-git/lancet/blob/main/validator/validator.go](https://github.com/duke-git/lancet/blob/main/validator/validator.go) - +- [https://github.com/duke-git/lancet/blob/main/validator/validator.go](https://github.com/duke-git/lancet/blob/main/validator/validator.go)
## 用法: + ```go import ( "github.com/duke-git/lancet/v2/validator" @@ -20,42 +21,42 @@ import (
## 目录: -- [ContainChinese](#ContainChinese) -- [ContainLetter](#ContainLetter) -- [ContainLower](#ContainLower) -- [ContainUpper](#ContainUpper) -- [IsAlpha](#IsAlpha) -- [IsAllUpper](#IsAllUpper) -- [IsAllLower](#IsAllLower) -- [IsBase64](#IsBase64) -- [IsChineseMobile](#IsChineseMobile) -- [IsChineseIdNum](#IsChineseIdNum) -- [IsChinesePhone](#IsChinesePhone) -- [IsCreditCard](#IsCreditCard) -- [IsDns](#IsDns) -- [IsEmail](#IsEmail) -- [IsEmptyString](#IsEmptyString) -- [IsFloatStr](#IsFloatStr) -- [IsNumberStr](#IsNumberStr) -- [IsJSON](#IsJSON) -- [IsRegexMatch](#IsRegexMatch) -- [IsIntStr](#IsIntStr) -- [IsIp](#IsIp) -- [IsIpV4](#IsIpV4) -- [IsIpV6](#IsIpV6) -- [IsStrongPassword](#IsStrongPassword) -- [IsUrl](#IsUrl) -- [IsWeakPassword](#IsWeakPassword) -- [IsZeroValue](#IsZeroValue) -- [IsGBK](#IsGBK) - + +- [ContainChinese](#ContainChinese) +- [ContainLetter](#ContainLetter) +- [ContainLower](#ContainLower) +- [ContainUpper](#ContainUpper) +- [IsAlpha](#IsAlpha) +- [IsAllUpper](#IsAllUpper) +- [IsAllLower](#IsAllLower) +- [IsBase64](#IsBase64) +- [IsChineseMobile](#IsChineseMobile) +- [IsChineseIdNum](#IsChineseIdNum) +- [IsChinesePhone](#IsChinesePhone) +- [IsCreditCard](#IsCreditCard) +- [IsDns](#IsDns) +- [IsEmail](#IsEmail) +- [IsEmptyString](#IsEmptyString) +- [IsFloatStr](#IsFloatStr) +- [IsNumberStr](#IsNumberStr) +- [IsJSON](#IsJSON) +- [IsRegexMatch](#IsRegexMatch) +- [IsIntStr](#IsIntStr) +- [IsIp](#IsIp) +- [IsIpV4](#IsIpV4) +- [IsIpV6](#IsIpV6) +- [IsStrongPassword](#IsStrongPassword) +- [IsUrl](#IsUrl) +- [IsWeakPassword](#IsWeakPassword) +- [IsZeroValue](#IsZeroValue) +- [IsGBK](#IsGBK)
## 文档 - ### ContainChinese +

验证字符串是否包含中文字符

函数签名: @@ -63,29 +64,33 @@ import ( ```go func ContainChinese(s string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.ContainChinese("你好") - fmt.Println(res1) //true + result1 := validator.ContainChinese("你好") + result2 := validator.ContainChinese("你好hello") + result3 := validator.ContainChinese("hello") - res2 := validator.ContainChinese("你好hello") - fmt.Println(res2) //true + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.ContainChinese("hello") - fmt.Println(res3) //false + // Output: + // true + // true + // false } ``` - - ### ContainLetter +

验证字符串是否包含至少一个英文字母

函数签名: @@ -93,30 +98,33 @@ func main() { ```go func ContainLetter(str string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.ContainLetter("1bc") - fmt.Println(res1) //true + result1 := validator.ContainLetter("你好") + result2 := validator.ContainLetter("&@#$%^&*") + result3 := validator.ContainLetter("ab1") - res2 := validator.ContainLetter("123") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.ContainLetter("&@#$%^&*") - fmt.Println(res3) //false + // Output: + // false + // false + // true } ``` - - - ### ContainLower +

验证字符串是否包含至少一个英文小写字母

函数签名: @@ -124,30 +132,33 @@ func main() { ```go func ContainLower(str string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.ContainLower("1bc") - fmt.Println(res1) //true + result1 := validator.ContainLower("abc") + result2 := validator.ContainLower("aBC") + result3 := validator.ContainLower("ABC") - res2 := validator.ContainLower("123") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.ContainLower("1BC") - fmt.Println(res3) //false + // Output: + // true + // true + // false } ``` - - - ### ContainUpper +

验证字符串是否包含至少一个英文大写字母.

函数签名: @@ -155,30 +166,33 @@ func main() { ```go func ContainUpper(str string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.ContainUpper("1bc") - fmt.Println(res1) //false + result1 := validator.ContainUpper("ABC") + result2 := validator.ContainUpper("abC") + result3 := validator.ContainUpper("abc") - res2 := validator.ContainUpper("123") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.ContainUpper("1BC") - fmt.Println(res3) //true + // Output: + // true + // true + // false } ``` - - - ### IsAlpha +

验证字符串是否只包含英文字母

函数签名: @@ -186,27 +200,33 @@ func main() { ```go func IsAlpha(s string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsAlpha("abc") - fmt.Println(res1) //true + result1 := validator.IsAlpha("abc") + result2 := validator.IsAlpha("ab1") + result3 := validator.IsAlpha("") - res2 := validator.IsAlpha("1bc") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.IsAlpha("") - fmt.Println(res3) //false + // Output: + // true + // false + // false } ``` ### IsAllUpper +

验证字符串是否全是大写英文字母

函数签名: @@ -214,27 +234,33 @@ func main() { ```go func IsAllUpper(str string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsAllUpper("ABC") - fmt.Println(res1) //true + result1 := validator.IsAllUpper("ABC") + result2 := validator.IsAllUpper("ABc") + result3 := validator.IsAllUpper("AB1") - res2 := validator.IsAllUpper("aBC") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false } ``` - - - ### IsAllLower +

验证字符串是否全是小写英文字母

函数签名: @@ -242,27 +268,33 @@ func main() { ```go func IsAllLower(str string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsAllLower("abc") - fmt.Println(res1) //true + result1 := validator.IsAllLower("abc") + result2 := validator.IsAllLower("abC") + result3 := validator.IsAllLower("ab1") - res2 := validator.IsAllLower("abC") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false } ``` - - - ### IsBase64 +

验证字符串是否是base64编码

函数签名: @@ -270,27 +302,30 @@ func main() { ```go func IsBase64(base64 string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsBase64("aGVsbG8=") - fmt.Println(res1) //true + result1 := validator.IsBase64("aGVsbG8=") + result2 := validator.IsBase64("123456") - res2 := validator.IsBase64("123456") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsChineseMobile +

验证字符串是否是中国手机号码

函数签名: @@ -298,26 +333,30 @@ func main() { ```go func IsChineseMobile(mobileNum string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsChineseMobile("13263527980") - fmt.Println(res1) //true + result1 := validator.IsChineseMobile("13263527980") + result2 := validator.IsChineseMobile("434324324") - res2 := validator.IsChineseMobile("434324324") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - ### IsChineseIdNum +

验证字符串是否是中国身份证号码

函数签名: @@ -325,27 +364,30 @@ func main() { ```go func IsChineseIdNum(id string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsChineseIdNum("210911192105130715") - fmt.Println(res1) //true + result1 := validator.IsChineseIdNum("210911192105130715") + result2 := validator.IsChineseIdNum("123456") - res2 := validator.IsChineseIdNum("123456") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsChinesePhone +

验证字符串是否是中国电话座机号码

函数签名: @@ -353,27 +395,30 @@ func main() { ```go func IsChinesePhone(phone string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsChinesePhone("010-32116675") - fmt.Println(res1) //true + result1 := validator.IsChinesePhone("010-32116675") + result2 := validator.IsChinesePhone("123-87562") - res2 := validator.IsChinesePhone("123-87562") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsCreditCard +

验证字符串是否是信用卡号码

函数签名: @@ -381,27 +426,30 @@ func main() { ```go func IsCreditCard(creditCart string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsCreditCard("4111111111111111") - fmt.Println(res1) //true + result1 := validator.IsCreditCard("4111111111111111") + result2 := validator.IsCreditCard("123456") - res2 := validator.IsCreditCard("123456") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsDns +

验证字符串是否是有效dns

函数签名: @@ -409,30 +457,33 @@ func main() { ```go func IsDns(dns string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsDns("abc.com") - fmt.Println(res1) //true + result1 := validator.IsDns("abc.com") + result2 := validator.IsDns("a.b.com") + result3 := validator.IsDns("http://abc.com") - res2 := validator.IsDns("a.b.com") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - res3 := validator.IsDns("http://abc.com") - fmt.Println(res3) //false + // Output: + // true + // false + // false } ``` - - - ### IsEmail +

验证字符串是否是有效电子邮件地址

函数签名: @@ -440,26 +491,30 @@ func main() { ```go func IsEmail(email string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsEmail("abc@xyz.com") - fmt.Println(res1) //true + result1 := validator.IsEmail("abc@xyz.com") + result2 := validator.IsEmail("a.b@@com") - res2 := validator.IsEmail("a.b@@com") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - ### IsEmptyString +

验证字符串是否是空字符串

函数签名: @@ -467,26 +522,33 @@ func main() { ```go func IsEmptyString(s string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - res1 := validator.IsEmptyString("") - fmt.Println(res1) //true + result1 := validator.IsEmptyString("") + result2 := validator.IsEmptyString(" ") + result3 := validator.IsEmptyString("\t") - res2 := validator.IsEmptyString("abc") - fmt.Println(res2) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false } ``` - - ### IsFloatStr +

验证字符串是否是可以转换为浮点数

函数签名: @@ -494,28 +556,36 @@ func main() { ```go func IsFloatStr(s string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsFloatStr("")) //false - fmt.Println(validator.IsFloatStr("12a")) //false - fmt.Println(validator.IsFloatStr("3.")) //true - fmt.Println(validator.IsFloatStr("+3.")) //true - fmt.Println(validator.IsFloatStr("-3.")) //true - fmt.Println(validator.IsFloatStr("12")) //true + result1 := validator.IsFloatStr("3.") + result2 := validator.IsFloatStr("+3.") + result3 := validator.IsFloatStr("12") + result4 := validator.IsFloatStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false } ``` - - - ### IsNumberStr +

验证字符串是否是可以转换为数字

函数签名: @@ -523,28 +593,36 @@ func main() { ```go func IsNumberStr(s string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsNumberStr("")) //false - fmt.Println(validator.IsNumberStr("12a")) //false - fmt.Println(validator.IsNumberStr("3.")) //true - fmt.Println(validator.IsNumberStr("+3.")) //true - fmt.Println(validator.IsNumberStr("-3.")) //true - fmt.Println(validator.IsNumberStr("+3e2")) //true + result1 := validator.IsNumberStr("3.") + result2 := validator.IsNumberStr("+3.") + result3 := validator.IsNumberStr("+3e2") + result4 := validator.IsNumberStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false } ``` - - - ### IsJSON +

验证字符串是否是有效json

函数签名: @@ -552,26 +630,36 @@ func main() { ```go func IsJSON(str string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsJSON("")) //false - fmt.Println(validator.IsJSON("abc")) //false - fmt.Println(validator.IsJSON("{}")) //true - fmt.Println(validator.IsJSON("[]")) //true - fmt.Println(validator.IsJSON("123")) //true - fmt.Println(validator.IsJSON("{\"name\": \"test\"}")) //true + result1 := validator.IsJSON("{}") + result2 := validator.IsJSON("{\"name\": \"test\"}") + result3 := validator.IsIntStr("") + result4 := validator.IsIntStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false } ``` - ### IsRegexMatch +

验证字符串是否可以匹配正则表达式

函数签名: @@ -579,24 +667,30 @@ func main() { ```go func IsRegexMatch(s, regex string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsRegexMatch("abc", `^[a-zA-Z]+$`)) //true - fmt.Println(validator.IsRegexMatch("1ab", `^[a-zA-Z]+$`)) //false - fmt.Println(validator.IsRegexMatch("", `^[a-zA-Z]+$`)) //false + result1 := validator.IsRegexMatch("abc", `^[a-zA-Z]+$`) + result2 := validator.IsRegexMatch("ab1", `^[a-zA-Z]+$`) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - ### IsIntStr +

验证字符串是否是可以转换为整数

函数签名: @@ -604,26 +698,36 @@ func main() { ```go func IsIntStr(s string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsIntStr("+3")) //true - fmt.Println(validator.IsIntStr("-3")) //true - fmt.Println(validator.IsIntStr("3.")) //false - fmt.Println(validator.IsIntStr("abc")) //false + result1 := validator.IsIntStr("+3") + result2 := validator.IsIntStr("-3") + result3 := validator.IsIntStr("3.") + result4 := validator.IsIntStr("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false } ``` - - - ### IsIp +

验证字符串是否是ip地址

函数签名: @@ -631,26 +735,36 @@ func main() { ```go func IsIp(ipstr string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsIp("127.0.0.1")) //true - fmt.Println(validator.IsIp("::0:0:0:0:0:0:1")) //true - fmt.Println(validator.IsIp("127.0.0")) //false - fmt.Println(validator.IsIp("127")) //false + result1 := validator.IsIp("127.0.0.1") + result2 := validator.IsIp("::0:0:0:0:0:0:1") + result3 := validator.IsIp("127.0.0") + result4 := validator.IsIp("::0:0:0:0:") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false } ``` - - - ### IsIpV4 +

验证字符串是否是ipv4地址

函数签名: @@ -658,26 +772,30 @@ func main() { ```go func IsIpV4(ipstr string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsIpV4("127.0.0.1")) //true - fmt.Println(validator.IsIpV4("::0:0:0:0:0:0:1")) //false - fmt.Println(validator.IsIpV4("127.0.0")) //false - fmt.Println(validator.IsIpV4("127")) //false + result1 := validator.IsIpV4("127.0.0.1") + result2 := validator.IsIpV4("::0:0:0:0:0:0:1") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsIpV6 +

验证字符串是否是ipv6地址

函数签名: @@ -685,26 +803,30 @@ func main() { ```go func IsIpV6(ipstr string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsIpV6("127.0.0.1")) //false - fmt.Println(validator.IsIpV6("::0:0:0:0:0:0:1")) //true - fmt.Println(validator.IsIpV6("127.0.0")) //false - fmt.Println(validator.IsIpV6("127")) //false + result1 := validator.IsIpV6("127.0.0.1") + result2 := validator.IsIpV6("::0:0:0:0:0:0:1") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true } ``` - - - ### IsStrongPassword +

验证字符串是否是强密码:(alpha(lower+upper) + number + special chars(!@#$%^&*()?><))

函数签名: @@ -712,54 +834,64 @@ func main() { ```go func IsStrongPassword(password string, length int) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsStrongPassword("abc", 3)) //false - fmt.Println(validator.IsStrongPassword("abc123", 6)) //false - fmt.Println(validator.IsStrongPassword("abcABC", 6)) //false - fmt.Println(validator.IsStrongPassword("abcABC123@#$", 16)) //false - fmt.Println(validator.IsStrongPassword("abcABC123@#$", 12)) //true + result1 := validator.IsStrongPassword("abcABC", 6) + result2 := validator.IsStrongPassword("abcABC123@#$", 10) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // false + // true } ``` - - - ### IsUrl +

验证字符串是否是url

函数签名: ```go -func IsUrl(str string) bool +func IsUrl(str string) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsUrl("http://abc.com")) //true - fmt.Println(validator.IsUrl("abc.com")) //true - fmt.Println(validator.IsUrl("a.b.com")) //true - fmt.Println(validator.IsUrl("abc")) //false + result1 := validator.IsUrl("abc.com") + result2 := validator.IsUrl("http://abc.com") + result3 := validator.IsUrl("abc") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false } ``` - - - ### IsWeakPassword +

验证字符串是否是弱密码:(only letter or only number or letter + number) .

@@ -768,26 +900,30 @@ func main() { ```go func IsWeakPassword(password string, length int) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsWeakPassword("abc")) //true - fmt.Println(validator.IsWeakPassword("123")) //true - fmt.Println(validator.IsWeakPassword("abc123")) //true - fmt.Println(validator.IsWeakPassword("abc123@#$")) //false + result1 := validator.IsWeakPassword("abcABC") + result2 := validator.IsWeakPassword("abc123@#$") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false } ``` - - - ### IsZeroValue +

判断传入的参数值是否为零值

函数签名: @@ -795,27 +931,36 @@ func main() { ```go func IsZeroValue(value any) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "github.com/duke-git/lancet/v2/validator" ) func main() { - fmt.Println(validator.IsZeroValue(nil)) //true - fmt.Println(validator.IsZeroValue(0)) //true - fmt.Println(validator.IsZeroValue("")) //true - fmt.Println(validator.IsZeroValue([]int)) //true - fmt.Println(validator.IsZeroValue(interface{})) //true - - fmt.Println(validator.IsZeroValue("0")) //false - fmt.Println(validator.IsZeroValue("nil")) //false + result1 := validator.IsZeroValue("") + result2 := validator.IsZeroValue(0) + result3 := validator.IsZeroValue("abc") + result4 := validator.IsZeroValue(1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false } ``` ### IsGBK +

检查数据编码是否为gbk(汉字内部代码扩展规范)。该函数的实现取决于双字节是否在gbk的编码范围内,而utf-8编码格式的每个字节都在gbk编码范围内。因此,应该首先调用utf8.valid检查它是否是utf-8编码,然后调用IsGBK检查gbk编码。如示例所示。

函数签名: @@ -823,23 +968,25 @@ func main() { ```go func IsGBK(data []byte) bool ``` + 例子: ```go import ( - "fmt" - "github.com/duke-git/lancet/v2/validator" + "fmt" + "golang.org/x/text/encoding/simplifiedchinese" + "github.com/duke-git/lancet/v2/validator" ) func main() { - data := []byte("你好") - - // 先检查utf8编码 - if utf8.Valid(data) { - fmt.Println("data encoding is utf-8") - }else if(validator.IsGBK(data)) { - fmt.Println("data encoding is GBK") - } - fmt.Println("data encoding is unknown") + str := "你好" + gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str)) + + result := validator.IsGBK(gbkData) + + fmt.Println(result) + + // Output: + // true } -``` \ No newline at end of file +```