package regx import ( "encoding/json" "regexp" "strconv" "unicode" ) // ContainChinese 验证字符串是否包含中文字符 func (rr *Regx) ContainChinese(s string) bool { var containChineseRegexMatcher = regexp.MustCompile("[\u4e00-\u9fa5]") return containChineseRegexMatcher.MatchString(s) } // IsAlpha checks if the string contains only letters (a-zA-Z) func (rr *Regx) IsAlpha(str string) bool { var isAlphaRegexMatcher = regexp.MustCompile(`^[a-zA-Z]+$`) return isAlphaRegexMatcher.MatchString(str) } // IsAllUpper check if the string is all upper case letters A-Z func (rr *Regx) IsAllUpper(str string) bool { for _, r := range str { if !unicode.IsUpper(r) { return false } } return str != "" } // IsAllLower check if the string is all lower case letters a-z func (rr *Regx) IsAllLower(str string) bool { for _, r := range str { if !unicode.IsLower(r) { return false } } return str != "" } // ContainUpper check if the string contain at least one upper case letter A-Z func (rr *Regx) ContainUpper(str string) bool { for _, r := range str { if unicode.IsUpper(r) && unicode.IsLetter(r) { return true } } return false } // ContainLower check if the string contain at least one lower case letter A-Z func (rr *Regx) ContainLower(str string) bool { for _, r := range str { if unicode.IsLower(r) && unicode.IsLetter(r) { return true } } return false } // ContainLetter check if the string contain at least one letter func (rr *Regx) ContainLetter(str string) bool { var containLetterRegexMatcher = regexp.MustCompile(`[a-zA-Z]`) return containLetterRegexMatcher.MatchString(str) } // IsJSON checks if the string is valid JSON func (rr *Regx) IsJSON(str string) bool { var js json.RawMessage return json.Unmarshal([]byte(str), &js) == nil } // IsFloatStr check if the string can convert to a float. func (rr *Regx) IsFloatStr(str string) bool { _, e := strconv.ParseFloat(str, 64) return e == nil } // IsIntStr check if the string can convert to a integer. func (rr *Regx) IsIntStr(str string) bool { var isIntStrRegexMatcher = regexp.MustCompile(`^[\+-]?\d+$`) return isIntStrRegexMatcher.MatchString(str) } // IsNumberStr check if the string can convert to a number. func (rr *Regx) IsNumberStr(s string) bool { return rr.IsIntStr(s) || rr.IsFloatStr(s) } // IsChineseIdNum check if the string is chinese id number. func IsChineseIdNum(id string) bool { var isChineseIdNumRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`) return isChineseIdNumRegexMatcher.MatchString(id) } // IsCreditCard check if the string is credit card. func IsCreditCard(creditCart string) bool { var isCreditCardRegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11}|6[27][0-9]{14})$`) return isCreditCardRegexMatcher.MatchString(creditCart) } // IsBase64 check if the string is base64 string. func IsBase64(base64 string) bool { var isBase64RegexMatcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`) return isBase64RegexMatcher.MatchString(base64) } // IsEmptyString check if the string is empty. func IsEmptyString(str string) bool { return len(str) == 0 } // IsRegexMatch check if the string match the regexp func IsRegexMatch(str, regex string) bool { reg := regexp.MustCompile(regex) return reg.MatchString(str) } // 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(!@#$%^&*()?><) func IsStrongPassword(password string, length int) bool { if len(password) < length { return false } var num, lower, upper, special bool for _, r := range password { switch { case unicode.IsDigit(r): num = true case unicode.IsUpper(r): upper = true case unicode.IsLower(r): lower = true case unicode.IsSymbol(r), unicode.IsPunct(r): special = true } } return num && lower && upper && special } // IsWeakPassword check if the string is weak password // Weak password: only letter or only number or letter + number func IsWeakPassword(password string) bool { var num, letter, special bool for _, r := range password { switch { case unicode.IsDigit(r): num = true case unicode.IsLetter(r): letter = true case unicode.IsSymbol(r), unicode.IsPunct(r): special = true } } return (num || letter) && !special }