# Validator | Lancet
A powerful util function library of Go

Skip to content
On this page

# Validator ​

Package validator contains some functions for data validation.

## Source: ​

- https://github.com/duke-git/lancet/blob/main/validator/validator.go

## Usage: ​

go

    import (
        "github.com/duke-git/lancet/v2/validator"
    )

    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

## Documentation ​

### ContainChinese ​

Check if the string contain mandarin chinese.

Signature:

go

    func ContainChinese(s string) bool

    func ContainChinese(s string) bool

Example:运行

go

    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
    }

    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:

go

    func ContainLetter(str string) bool

    func ContainLetter(str string) bool

Example:运行

go

    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
    }

    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:

go

    func ContainLower(str string) bool

    func ContainLower(str string) bool

Example:运行

go

    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
    }

    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:

go

    func ContainUpper(str string) bool

    func ContainUpper(str string) bool

Example:运行

go

    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
    }

    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:

go

    func IsAlpha(s string) bool

    func IsAlpha(s string) bool

Example:运行

go

    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
    }

    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:

go

    func IsAllUpper(str string) bool

    func IsAllUpper(str string) bool

Example:运行

go

    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
    }

    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:

go

    func IsAllLower(str string) bool

    func IsAllLower(str string) bool

Example:运行

go

    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
    }

    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:

go

    func IsASCII(str string) bool

    func IsASCII(str string) bool

Example:运行

go

    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
    }

    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:

go

    func IsBase64(base64 string) bool

    func IsBase64(base64 string) bool

Example:运行

go

    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
    }

    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:

go

    func IsChineseMobile(mobileNum string) bool

    func IsChineseMobile(mobileNum string) bool

Example:运行

go

    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
    }

    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:

go

    func IsChineseIdNum(id string) bool

    func IsChineseIdNum(id string) bool

Example:运行

go

    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
    }

    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:

go

    func IsChinesePhone(phone string) bool

    func IsChinesePhone(phone string) bool

Example:运行

go

    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
    }

    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:

go

    func IsCreditCard(creditCart string) bool

    func IsCreditCard(creditCart string) bool

Example:运行

go

    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
    }

    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:

go

    func IsDns(dns string) bool

    func IsDns(dns string) bool

Example:运行

go

    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
    }

    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:

go

    func IsEmail(email string) bool

    func IsEmail(email string) bool

Example:运行

go

    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
    }

    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:

go

    func IsEmptyString(s string) bool

    func IsEmptyString(s string) bool

Example:运行

go

    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
    }

    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:

go

    func IsInt(v any) bool

    func IsInt(v any) bool

Example:运行

go

    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
    }

    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:

go

    func IsFloat(v any) bool

    func IsFloat(v any) bool

Example:运行

go

    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
    }

    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:

go

    func IsNumber(v any) bool

    func IsNumber(v any) bool

Example:运行

go

    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
    }

    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:

go

    func IsIntStr(s string) bool

    func IsIntStr(s string) bool

Example:运行

go

    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
    }

    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:

go

    func IsFloatStr(s string) bool

    func IsFloatStr(s string) bool

Example:运行

go

    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
    }

    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:

go

    func IsNumberStr(s string) bool

    func IsNumberStr(s string) bool

Example:运行

go

    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
    }

    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:

go

    func IsJSON(str string) bool

    func IsJSON(str string) bool

Example:运行

go

    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
    }

    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:

go

    func IsRegexMatch(s, regex string) bool

    func IsRegexMatch(s, regex string) bool

Example:运行

go

    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
    }

    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:

go

    func IsIp(ipstr string) bool

    func IsIp(ipstr string) bool

Example:运行

go

    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
    }

    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:

go

    func IsIpV4(ipstr string) bool

    func IsIpV4(ipstr string) bool

Example:运行

go

    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
    }

    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:

go

    func IsIpV6(ipstr string) bool

    func IsIpV6(ipstr string) bool

Example:运行

go

    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
    }

    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:

go

    func IsStrongPassword(password string, length int) bool

    func IsStrongPassword(password string, length int) bool

Example:运行

go

    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
    }

    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:

go

    func IsUrl(str string) bool

    func IsUrl(str string) bool

Example:运行

go

    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
    }

    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:

go

    func IsWeakPassword(password string, length int) bool

    func IsWeakPassword(password string, length int) bool

Example:运行

go

    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
    }

    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:

go

    func IsZeroValue(value any) bool

    func IsZeroValue(value any) bool

Example:运行

go

    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
    }

    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:

go

    func IsGBK(data []byte) bool

    func IsGBK(data []byte) bool

Example:运行

go

    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
    }

    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:

go

    func IsPrintable(str string) bool

    func IsPrintable(str string) bool

Example:运行

go

    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
    }

    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
    }