1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
Files
lancet/docs/validator_zh-CN.md
2023-02-23 10:36:54 +08:00

15 KiB
Raw Blame History

Validator

validator 验证器包,包含常用字符串格式验证函数。

源码:

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

用法:

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

目录:

文档

ContainChinese

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

函数签名:

func ContainChinese(s string) bool

例子:

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

func main() {
    res1 := validator.ContainChinese("你好")
    fmt.Println(res1) //true

    res2 := validator.ContainChinese("你好hello")
    fmt.Println(res2) //true

    res3 := validator.ContainChinese("hello")
    fmt.Println(res3) //false
}

ContainLetter

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

函数签名:

func ContainLetter(str string) bool

例子:

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

func main() {
    res1 := validator.ContainLetter("1bc")
    fmt.Println(res1) //true

    res2 := validator.ContainLetter("123")
    fmt.Println(res2) //false

    res3 := validator.ContainLetter("&@#$%^&*")
    fmt.Println(res3) //false
}

ContainLower

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

函数签名:

func ContainLower(str string) bool

例子:

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

func main() {
    res1 := validator.ContainLower("1bc")
    fmt.Println(res1) //true

    res2 := validator.ContainLower("123")
    fmt.Println(res2) //false

    res3 := validator.ContainLower("1BC")
    fmt.Println(res3) //false
}

ContainUpper

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

函数签名:

func ContainUpper(str string) bool

例子:

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

func main() {
    res1 := validator.ContainUpper("1bc")
    fmt.Println(res1) //false

    res2 := validator.ContainUpper("123")
    fmt.Println(res2) //false

    res3 := validator.ContainUpper("1BC")
    fmt.Println(res3) //true
}

IsAlpha

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

函数签名:

func IsAlpha(s string) bool

例子:

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

func main() {
    res1 := validator.IsAlpha("abc")
    fmt.Println(res1) //true

    res2 := validator.IsAlpha("1bc")
    fmt.Println(res2) //false

    res3 := validator.IsAlpha("")
    fmt.Println(res3) //false
}

IsAllUpper

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

函数签名:

func IsAllUpper(str string) bool

例子:

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

func main() {
    res1 := validator.IsAllUpper("ABC")
    fmt.Println(res1) //true

    res2 := validator.IsAllUpper("aBC")
    fmt.Println(res2) //false
}

IsAllLower

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

函数签名:

func IsAllLower(str string) bool

例子:

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

func main() {
    res1 := validator.IsAllLower("abc")
    fmt.Println(res1) //true

    res2 := validator.IsAllLower("abC")
    fmt.Println(res2) //false
}

IsBase64

验证字符串是否是base64编码

函数签名:

func IsBase64(base64 string) bool

例子:

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

func main() {
    res1 := validator.IsBase64("aGVsbG8=")
    fmt.Println(res1) //true

    res2 := validator.IsBase64("123456")
    fmt.Println(res2) //false
}

IsChineseMobile

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

函数签名:

func IsChineseMobile(mobileNum string) bool

例子:

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

func main() {
    res1 := validator.IsChineseMobile("13263527980")
    fmt.Println(res1) //true

    res2 := validator.IsChineseMobile("434324324")
    fmt.Println(res2) //false
}

IsChineseIdNum

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

函数签名:

func IsChineseIdNum(id string) bool

例子:

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

func main() {
    res1 := validator.IsChineseIdNum("210911192105130715")
    fmt.Println(res1) //true

    res2 := validator.IsChineseIdNum("123456")
    fmt.Println(res2) //false
}

IsChinesePhone

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

函数签名:

func IsChinesePhone(phone string) bool

例子:

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

func main() {
    res1 := validator.IsChinesePhone("010-32116675")
    fmt.Println(res1) //true

    res2 := validator.IsChinesePhone("123-87562")
    fmt.Println(res2) //false
}

IsCreditCard

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

函数签名:

func IsCreditCard(creditCart string) bool

例子:

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

func main() {
    res1 := validator.IsCreditCard("4111111111111111")
    fmt.Println(res1) //true

    res2 := validator.IsCreditCard("123456")
    fmt.Println(res2) //false
}

IsDns

验证字符串是否是有效dns

函数签名:

func IsDns(dns string) bool

例子:

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

func main() {
    res1 := validator.IsDns("abc.com")
    fmt.Println(res1) //true

    res2 := validator.IsDns("a.b.com")
    fmt.Println(res2) //false

    res3 := validator.IsDns("http://abc.com")
    fmt.Println(res3) //false
}

IsEmail

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

函数签名:

func IsEmail(email string) bool

例子:

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

func main() {
    res1 := validator.IsEmail("abc@xyz.com")
    fmt.Println(res1) //true

    res2 := validator.IsEmail("a.b@@com")
    fmt.Println(res2) //false
}

IsEmptyString

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

函数签名:

func IsEmptyString(s string) bool

例子:

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

func main() {
    res1 := validator.IsEmptyString("")
    fmt.Println(res1) //true

    res2 := validator.IsEmptyString("abc")
    fmt.Println(res2) //false
}

IsFloatStr

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

函数签名:

func IsFloatStr(s string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsNumberStr

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

函数签名:

func IsNumberStr(s string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsJSON

验证字符串是否是有效json

函数签名:

func IsJSON(str string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsRegexMatch

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

函数签名:

func IsRegexMatch(s, regex string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsIntStr

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

函数签名:

func IsIntStr(s string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsIp

验证字符串是否是ip地址

函数签名:

func IsIp(ipstr string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsIpV4

验证字符串是否是ipv4地址

函数签名:

func IsIpV4(ipstr string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsIpV6

验证字符串是否是ipv6地址

函数签名:

func IsIpV6(ipstr string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsStrongPassword

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

函数签名:

func IsStrongPassword(password string, length int) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsUrl

验证字符串是否是url

函数签名:

func IsUrl(str string) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsWeakPassword

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

函数签名:

func IsWeakPassword(password string, length int) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsZeroValue

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

函数签名:

func IsZeroValue(value any) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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
}

IsGBK

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

函数签名:

func IsGBK(data []byte) bool

例子:

import (
    "fmt"
    "github.com/duke-git/lancet/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")
}