1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-10 07:42:27 +08:00

feat: add ContainNumber for validator (#97)

Co-authored-by: sunyaoyao <sunyaoyao@kezaihui.com>
This commit is contained in:
will
2023-05-19 11:23:19 +08:00
committed by GitHub
parent 3beb769f09
commit 78aa679670
3 changed files with 42 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ import (
var (
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
numberRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d`)
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
@@ -111,6 +112,12 @@ func ContainLetter(str string) bool {
return letterRegexMatcher.MatchString(str)
}
// ContainLetter check if the string contain at least one number.
func ContainNumber(input string) bool {
return numberRegexMatcher.MatchString(input)
}
// IsJSON checks if the string is valid JSON.
// Play: https://go.dev/play/p/8Kip1Itjiil
func IsJSON(str string) bool {