validator

package
v0.0.0-...-654d6e8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package validator implements some validate function for string.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainChinese

func ContainChinese(s string) bool

ContainChinese check if the string contain mandarin chinese. Play: https://go.dev/play/p/7DpU0uElYeM

Example
result1 := ContainChinese("你好")
result2 := ContainChinese("你好hello")
result3 := ContainChinese("hello")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
true
false

func ContainLetter

func ContainLetter(str string) bool

ContainLetter check if the string contain at least one letter. Play: https://go.dev/play/p/lqFD04Yyewp

Example
result1 := ContainLetter("你好")
result2 := ContainLetter("&@#$%^&*")
result3 := ContainLetter("ab1")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

false
false
true

func ContainLower

func ContainLower(str string) bool

ContainLower check if the string contain at least one lower case letter a-z. Play: https://go.dev/play/p/Srqi1ItvnAA

Example
result1 := ContainLower("abc")
result2 := ContainLower("aBC")
result3 := ContainLower("ABC")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
true
false

func ContainNumber

func ContainNumber(input string) bool

ContainNumber check if the string contain at least one number.

Example
result1 := ContainNumber("你好")
result2 := ContainNumber("&@#$%^&*")
result3 := ContainNumber("ab1")
result4 := ContainNumber("1234")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

false
false
true
true

func ContainUpper

func ContainUpper(str string) bool

ContainUpper check if the string contain at least one upper case letter A-Z. Play: https://go.dev/play/p/CmWeBEk27-z

Example
result1 := ContainUpper("ABC")
result2 := ContainUpper("abC")
result3 := ContainUpper("abc")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
true
false

func IsASCII

func IsASCII(str string) bool

IsASCII checks if string is all ASCII char. Play: https://go.dev/play/p/hfQNPLX0jNa

Example
result1 := IsASCII("ABC")
result2 := IsASCII("123")
result3 := IsASCII("")
result4 := IsASCII("😄")
result5 := IsASCII("你好")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
Output:

true
true
true
false
false

func IsAllLower

func IsAllLower(str string) bool

IsAllLower check if the string is all lower case letters a-z. Play: https://go.dev/play/p/GjqCnOfV6cM

Example
result1 := IsAllLower("abc")
result2 := IsAllLower("abC")
result3 := IsAllLower("ab1")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
false
false

func IsAllUpper

func IsAllUpper(str string) bool

IsAllUpper check if the string is all upper case letters A-Z. Play: https://go.dev/play/p/ZHctgeK1n4Z

Example
result1 := IsAllUpper("ABC")
result2 := IsAllUpper("ABc")
result3 := IsAllUpper("AB1")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
false
false

func IsAlpha

func IsAlpha(str string) bool

IsAlpha checks if the string contains only letters (a-zA-Z). Play: https://go.dev/play/p/7Q5sGOz2izQ

Example
result1 := IsAlpha("abc")
result2 := IsAlpha("ab1")
result3 := IsAlpha("")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
false
false

func IsAmericanExpress

func IsAmericanExpress(v string) bool

IsAmericanExpress check if a give string is a valid american expression card nubmer or not. Play: https://go.dev/play/p/HIDFpcOdpkd

Example
result1 := IsAmericanExpress("342883359122187")
result2 := IsAmericanExpress("3782822463100007")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsBase64

func IsBase64(base64 string) bool

IsBase64 check if the string is base64 string. Play: https://go.dev/play/p/sWHEySAt6hl

Example
result1 := IsBase64("aGVsbG8=")
result2 := IsBase64("123456")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsBase64URL

func IsBase64URL(v string) bool

IsBase64URL check if a give string is a valid URL-safe Base64 encoded string. Play: https://go.dev/play/p/vhl4mr8GZ6S

Example
result1 := IsBase64URL("SAGsbG8sIHdvcmxkIQ")
result2 := IsBase64URL("SAGsbG8sIHdvcmxkIQ==")
result3 := IsBase64URL("SAGsbG8sIHdvcmxkIQ=")
result4 := IsBase64URL("SAGsbG8sIHdvcmxkIQ===")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
false
false

func IsBin

func IsBin(v string) bool

IsBin check if a give string is a valid binary value or not. Play: https://go.dev/play/p/ogPeg2XJH4P

Example
result1 := IsBin("0101")
result2 := IsBin("0b1101")
result3 := IsBin("b1101")
result4 := IsBin("1201")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
false
false

func IsChinaUnionPay

func IsChinaUnionPay(v string) bool

IsChinaUnionPay check if a give string is a valid china union pay nubmer or not. Play: https://go.dev/play/p/yafpdxLiymu

Example
result1 := IsChinaUnionPay("6250941006528599")
result2 := IsChinaUnionPay("3782822463100007")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsChineseIdNum

func IsChineseIdNum(id string) bool

IsChineseIdNum check if the string is chinese id card. Play: https://go.dev/play/p/d8EWhl2UGDF

Example
result1 := IsChineseIdNum("210911192105130714")
result2 := IsChineseIdNum("123456")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsChineseMobile

func IsChineseMobile(mobileNum string) bool

IsChineseMobile check if the string is chinese mobile number. Play: https://go.dev/play/p/GPYUlGTOqe3

Example
result1 := IsChineseMobile("13263527980")
result2 := IsChineseMobile("434324324")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsChinesePhone

func IsChinesePhone(phone string) bool

IsChinesePhone check if the string is chinese phone number. Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx. Play: https://go.dev/play/p/RUD_-7YZJ3I

Example
result1 := IsChinesePhone("010-32116675")
result2 := IsChinesePhone("123-87562")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsCreditCard

func IsCreditCard(creditCart string) bool

IsCreditCard check if the string is credit card. Play: https://go.dev/play/p/sNwwL6B0-v4

Example
result1 := IsCreditCard("4111111111111111")
result2 := IsCreditCard("123456")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsDns

func IsDns(dns string) bool

IsDns check if the string is dns. Play: https://go.dev/play/p/jlYApVLLGTZ

Example
result1 := IsDns("abc.com")
result2 := IsDns("a.b.com")
result3 := IsDns("http://abc.com")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
true
false

func IsEmail

func IsEmail(email string) bool

IsEmail check if the string is a email address. Play: https://go.dev/play/p/Os9VaFlT33G

Example
result1 := IsEmail("abc@xyz.com")
result2 := IsEmail("a.b@@com")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsEmptyString

func IsEmptyString(str string) bool

IsEmptyString check if the string is empty. Play: https://go.dev/play/p/dpzgUjFnBCX

Example
result1 := IsEmptyString("")
result2 := IsEmptyString(" ")
result3 := IsEmptyString("\t")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
false
false

func IsFloat

func IsFloat(v any) bool

IsFloat check if the value is float(float32, float34) or not. Play: https://go.dev/play/p/vsyG-sxr99_Z

Example
result1 := IsFloat("")
result2 := IsFloat("3")
result3 := IsFloat(0)
result4 := IsFloat(0.1)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

false
false
false
true

func IsFloatStr

func IsFloatStr(str string) bool

IsFloatStr check if the string can convert to a float. Play: https://go.dev/play/p/LOYwS_Oyl7U

Example
result1 := IsFloatStr("3.")
result2 := IsFloatStr("+3.")
result3 := IsFloatStr("12")
result4 := IsFloatStr("abc")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
true
false

func IsGBK

func IsGBK(data []byte) bool

IsGBK check if data encoding is gbk Note: 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 below *

data := []byte("你好")
if utf8.Valid(data) {
	fmt.Println("data encoding is utf-8")
}else if(IsGBK(data)) {
	fmt.Println("data encoding is GBK")
}
fmt.Println("data encoding is unknown")

* Play: https://go.dev/play/p/E2nt3unlmzP

Example
str := "你好"
gbkData, _ := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(str))

result := IsGBK(gbkData)

fmt.Println(result)
Output:

true

func IsHex

func IsHex(v string) bool

IsHex check if a give string is a valid hexadecimal value or not. Play: https://go.dev/play/p/M2qpHbEwmm7

Example
result1 := IsHex("0xabcde")
result2 := IsHex("0XABCDE")
result3 := IsHex("cdfeg")
result4 := IsHex("0xcdfeg")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
false
false

func IsInt

func IsInt(v any) bool

IsInt check if the value is integer(int, unit) or not. Play: https://go.dev/play/p/eFoIHbgzl-z

Example
result1 := IsInt("")
result2 := IsInt("3")
result3 := IsInt(0.1)
result4 := IsInt(0)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

false
false
false
true

func IsIntStr

func IsIntStr(str string) bool

IsIntStr check if the string can convert to a integer. Play: https://go.dev/play/p/jQRtFv-a0Rk

Example
result1 := IsIntStr("+3")
result2 := IsIntStr("-3")
result3 := IsIntStr("3.")
result4 := IsIntStr("abc")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
false
false

func IsIp

func IsIp(ipstr string) bool

IsIp check if the string is a ip address. Play: https://go.dev/play/p/FgcplDvmxoD

Example
result1 := IsIp("127.0.0.1")
result2 := IsIp("::0:0:0:0:0:0:1")
result3 := IsIp("127.0.0")
result4 := IsIp("::0:0:0:0:")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
false
false

func IsIpV4

func IsIpV4(ipstr string) bool

IsIpV4 check if the string is a ipv4 address. Play: https://go.dev/play/p/zBGT99EjaIu

Example
result1 := IsIpV4("127.0.0.1")
result2 := IsIpV4("::0:0:0:0:0:0:1")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsIpV6

func IsIpV6(ipstr string) bool

IsIpV6 check if the string is a ipv6 address. Play: https://go.dev/play/p/AHA0r0AzIdC

Example
result1 := IsIpV6("127.0.0.1")
result2 := IsIpV6("::0:0:0:0:0:0:1")

fmt.Println(result1)
fmt.Println(result2)
Output:

false
true

func IsJSON

func IsJSON(str string) bool

IsJSON checks if the string is valid JSON. Play: https://go.dev/play/p/8Kip1Itjiil

Example
result1 := IsJSON("{}")
result2 := IsJSON("{\"name\": \"test\"}")
result3 := IsJSON("")
result4 := IsJSON("abc")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
false
false

func IsJWT

func IsJWT(v string) bool

IsJWT check if a give string is a valid JSON Web Token (JWT). Play: https://go.dev/play/p/R6Op7heJbKI

Example
result1 := IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910")
result2 := IsJWT("abc")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsMasterCard

func IsMasterCard(v string) bool

IsMasterCard check if a give string is a valid master card nubmer or not. Play: https://go.dev/play/p/CwWBFRrG27b

Example
result1 := IsMasterCard("5425233430109903")
result2 := IsMasterCard("4111111111111111")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsNumber

func IsNumber(v any) bool

IsNumber check if the value is number(integer, float) or not. Play: https://go.dev/play/p/mdJHOAvtsvF

Example
result1 := IsNumber("")
result2 := IsNumber("3")
result3 := IsNumber(0)
result4 := IsNumber(0.1)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

false
false
true
true

func IsNumberStr

func IsNumberStr(s string) bool

IsNumberStr check if the string can convert to a number. Play: https://go.dev/play/p/LzaKocSV79u

Example
result1 := IsNumberStr("3.")
result2 := IsNumberStr("+3.")
result3 := IsNumberStr("+3e2")
result4 := IsNumberStr("abc")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
true
false

func IsPort

func IsPort(str string) bool

IsPort check if the string is a valid net port. Play:

func IsPrintable

func IsPrintable(str string) bool

IsPrintable checks if string is all printable chars. Play: https://go.dev/play/p/Pe1FE2gdtTP

Example
result1 := IsPrintable("ABC")
result2 := IsPrintable("{id: 123}")
result3 := IsPrintable("")
result4 := IsPrintable("😄")
result5 := IsPrintable("\u0000")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
Output:

true
true
true
true
false

func IsRegexMatch

func IsRegexMatch(str, regex string) bool

IsRegexMatch check if the string match the regexp. Play: https://go.dev/play/p/z_XeZo_litG

Example
result1 := IsRegexMatch("abc", `^[a-zA-Z]+$`)
result2 := IsRegexMatch("ab1", `^[a-zA-Z]+$`)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsStrongPassword

func IsStrongPassword(password string, length int) bool

IsStrongPassword check if the string is strong password, if len(password) is less than the length param, return false Strong password: alpha(lower+upper) + number + special chars(!@#$%^&*()?><). Play: https://go.dev/play/p/QHdVcSQ3uDg

Example
result1 := IsStrongPassword("abcABC", 6)
result2 := IsStrongPassword("abcABC123@#$", 10)

fmt.Println(result1)
fmt.Println(result2)
Output:

false
true

func IsUnionPay

func IsUnionPay(v string) bool

IsUnionPay check if a give string is a valid union pay nubmer or not. Play: https://go.dev/play/p/CUHPEwEITDf

Example
result1 := IsUnionPay("6221263430109903")
result2 := IsUnionPay("3782822463100007")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsUrl

func IsUrl(str string) bool

IsUrl check if the string is url. Play: https://go.dev/play/p/pbJGa7F98Ka

Example
result1 := IsUrl("abc.com")
result2 := IsUrl("http://abc.com")
result3 := IsUrl("abc")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

true
true
false

func IsVisa

func IsVisa(v string) bool

IsVisa check if a give string is a valid visa card nubmer or not. Play: https://go.dev/play/p/SdS2keOyJsl

Example
result1 := IsVisa("4111111111111111")
result2 := IsVisa("123")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsWeakPassword

func IsWeakPassword(password string) bool

IsWeakPassword check if the string is weak password Weak password: only letter or only number or letter + number. Play: https://go.dev/play/p/wqakscZH5gH

Example
result1 := IsWeakPassword("abcABC")
result2 := IsWeakPassword("abc123@#$")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func IsZeroValue

func IsZeroValue(value any) bool

IsZeroValue checks if value is a zero value. Play: https://go.dev/play/p/UMrwaDCi_t4

Example
result1 := IsZeroValue("")
result2 := IsZeroValue(0)
result3 := IsZeroValue("abc")
result4 := IsZeroValue(1)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
false
false

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL