Validator
Package validator contains some functions for data validation.
Source:
Usage:
import (
"github.com/duke-git/lancet/v2/validator"
)
Index
- ContainChinese
- ContainLetter
- ContainLower
- ContainUpper
- IsAlpha
- IsAllUpper
- IsAllLower
- IsASCII
- IsBase64
- IsChineseMobile
- IsChineseIdNum
- IsChinesePhone
- IsCreditCard
- IsDns
- IsEmail
- IsEmptyString
- IsInt
- IsFloat
- IsNumber
- IsIntStr
- IsFloatStr
- IsNumberStr
- IsJSON
- IsRegexMatch
- IsIp
- IsIpV4
- IsIpV6
- IsStrongPassword
- IsUrl
- IsWeakPassword
- IsZeroValue
- IsGBK
- IsPrintable
- IsBin
- IsHex
- IsBase64URL
- IsJWT
- IsVisa
- IsMasterCard
- IsAmericanExpress
- IsUnionPay
- IsChinaUnionPay
Documentation
ContainChinese
Check if the string contain mandarin chinese.
Signature:
func ContainChinese(s string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.ContainChinese("你好")
result2 := validator.ContainChinese("你好hello")
result3 := validator.ContainChinese("hello")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// true
// false
}
ContainLetter
Check if the string contain at least one letter.
Signature:
func ContainLetter(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.ContainLetter("你好")
result2 := validator.ContainLetter("&@#$%^&*")
result3 := validator.ContainLetter("ab1")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// false
// false
// true
}
ContainLower
Check if the string contain at least one lower case letter a-z.
Signature:
func ContainLower(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.ContainLower("abc")
result2 := validator.ContainLower("aBC")
result3 := validator.ContainLower("ABC")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// true
// false
}
ContainUpper
Check if the string contain at least one upper case letter A-Z.
Signature:
func ContainUpper(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.ContainUpper("ABC")
result2 := validator.ContainUpper("abC")
result3 := validator.ContainUpper("abc")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// true
// false
}
IsAlpha
Check if the string contains only letters (a-zA-Z).
Signature:
func IsAlpha(s string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsAlpha("abc")
result2 := validator.IsAlpha("ab1")
result3 := validator.IsAlpha("")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// false
// false
}
IsAllUpper
Check if string is all upper case letters A-Z.
Signature:
func IsAllUpper(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsAllUpper("ABC")
result2 := validator.IsAllUpper("ABc")
result3 := validator.IsAllUpper("AB1")
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:
func IsAllLower(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsAllLower("abc")
result2 := validator.IsAllLower("abC")
result3 := validator.IsAllLower("ab1")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// false
// false
}
IsASCII
Checks if string is all ASCII char.
Signature:
func IsASCII(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsASCII("ABC")
result2 := validator.IsASCII("123")
result3 := validator.IsASCII("")
result4 := validator.IsASCII("😄")
result5 := validator.IsASCII("你好")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// false
// false
}
IsBase64
Check if the string is base64 string.
Signature:
func IsBase64(base64 string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsBase64("aGVsbG8=")
result2 := validator.IsBase64("123456")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsChineseMobile
Check if the string is valid chinese mobile number.
Signature:
func IsChineseMobile(mobileNum string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsChineseMobile("13263527980")
result2 := validator.IsChineseMobile("434324324")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsChineseIdNum
Check if the string is chinese id number.
Signature:
func IsChineseIdNum(id string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsChineseIdNum("210911192105130715")
result2 := validator.IsChineseIdNum("123456")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsChinesePhone
Check if the string is chinese phone number.
Signature:
func IsChinesePhone(phone string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsChinesePhone("010-32116675")
result2 := validator.IsChinesePhone("123-87562")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsCreditCard
Check if the string is credit card.
Signature:
func IsCreditCard(creditCart string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsCreditCard("4111111111111111")
result2 := validator.IsCreditCard("123456")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsDns
Check if the string is valid dns.
Signature:
func IsDns(dns string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsDns("abc.com")
result2 := validator.IsDns("a.b.com")
result3 := validator.IsDns("http://abc.com")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// false
// false
}
IsEmail
Check if the string is email address.
Signature:
func IsEmail(email string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsEmail("abc@xyz.com")
result2 := validator.IsEmail("a.b@@com")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsEmptyString
Check if the string is empty or not.
Signature:
func IsEmptyString(s string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsEmptyString("")
result2 := validator.IsEmptyString(" ")
result3 := validator.IsEmptyString("\t")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// true
// false
// false
}
IsInt
Check if the value is integer(int, unit) or not.
Signature:
func IsInt(v any) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsInt("")
result2 := validator.IsInt("3")
result3 := validator.IsInt(0.1)
result4 := validator.IsInt(0)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// false
// false
// false
// true
}
IsFloat
Check if the value is float(float32, float34) or not.
Signature:
func IsFloat(v any) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsFloat("")
result2 := validator.IsFloat("3")
result3 := validator.IsFloat(0)
result4 := validator.IsFloat(0.1)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// false
// false
// false
// true
}
IsNumber
Check if the value is number(integer, float) or not.
Signature:
func IsNumber(v any) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsNumber("")
result2 := validator.IsNumber("3")
result3 := validator.IsNumber(0.1)
result4 := validator.IsNumber(0)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// false
// false
// true
// true
}
IsIntStr
Check if the string can convert to a integer.
Signature:
func IsIntStr(s string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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
}
IsFloatStr
Check if the string can convert to a float.
Signature:
func IsFloatStr(s string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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:
func IsNumberStr(s string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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:
func IsJSON(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsJSON("{}")
result2 := validator.IsJSON("{\"name\": \"test\"}")
result3 := validator.IsJSON("")
result4 := validator.IsJSON("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:
func IsRegexMatch(s, regex string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsRegexMatch("abc", `^[a-zA-Z]+$`)
result2 := validator.IsRegexMatch("ab1", `^[a-zA-Z]+$`)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsIp
Check if the string is a ip address.
Signature:
func IsIp(ipstr string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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:
func IsIpV4(ipstr string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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:
func IsIpV6(ipstr string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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(!@#$%^&*()?gt<)).
Signature:
func IsStrongPassword(password string, length int) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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:
func IsUrl(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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) .
Signature:
func IsWeakPassword(password string, length int) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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:
func IsZeroValue(value any) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
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
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:
func IsGBK(data []byte) bool
Example:Run
import (
"fmt"
"golang.org/x/text/encoding/simplifiedchinese"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
str := "你好"
gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str))
result := validator.IsGBK(gbkData)
fmt.Println(result)
// Output:
// true
}
IsPrintable
Checks if string is all printable chars.
Signature:
func IsPrintable(str string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsPrintable("ABC")
result2 := validator.IsPrintable("{id: 123}")
result3 := validator.IsPrintable("")
result4 := validator.IsPrintable("😄")
result5 := validator.IsPrintable("\u0000")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// true
// true
// true
// true
// false
}
IsBin
Checks if a give string is a valid binary value or not.
Signature:
func IsBin(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsBin("0101")
result2 := validator.IsBin("0b1101")
result3 := validator.IsBin("b1101")
result4 := validator.IsBin("1201")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// true
// true
// false
// false
}
IsHex
Checks if a give string is a valid hexadecimal value or not.
Signature:
func IsHex(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsHex("0xabcde")
result2 := validator.IsHex("0XABCDE")
result3 := validator.IsHex("cdfeg")
result4 := validator.IsHex("0xcdfeg")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// true
// true
// false
// false
}
IsBase64URL
Checks if a give string is a valid URL-safe Base64 encoded string.
Signature:
func IsBase64URL(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ")
result2 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ==")
result3 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ=")
result4 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ===")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// true
// true
// false
// false
}
IsJWT
Checks if a give string is is a valid JSON Web Token (JWT).
Signature:
func IsJWT(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910")
result2 := validator.IsJWT("abc")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsVisa
Checks if a give string is a valid visa card nubmer or not.
Signature:
func IsVisa(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsVisa("4111111111111111")
result2 := validator.IsVisa("123")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsMasterCard
Checks if a give string is a valid mastercard nubmer or not.
Signature:
func IsMasterCard(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsMasterCard("5425233430109903")
result2 := validator.IsMasterCard("4111111111111111")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsAmericanExpress
Checks if a give string is a valid american express nubmer or not.
Signature:
func IsAmericanExpress(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsAmericanExpress("342883359122187")
result2 := validator.IsAmericanExpress("3782822463100007")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsVisa
Checks if a give string is a valid union pay nubmer or not.
Signature:
func IsUnionPay(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsUnionPay("6221263430109903")
result2 := validator.IsUnionPay("3782822463100007")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
IsChinaUnionPay
Checks if a give string is a valid china union pay nubmer or not.
Signature:
func IsChinaUnionPay(v string) bool
Example:Run
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
result1 := validator.IsChinaUnionPay("6250941006528599")
result2 := validator.IsChinaUnionPay("3782822463100007")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}