You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package validate
/***********************************************************
*名字 golang 正则工具
*功能 支持数字,字母,字符,常用信息(电话,邮箱)等的正则匹配
************************************************************/
import (
"regexp"
"strings"
)
func IsColor(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$", s)
if false == b {
return b
}
}
return b
}
func IsURL(s string) bool {
if strings.HasPrefix(s, "http://") {
return true
}
if strings.HasPrefix(s, "https://") {
return true
}
return false
}
/************************* 自定义类型 ************************/
//数字+字母 不限制大小写 6~30位
func IsID(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^[0-9a-zA-Z]{6,30}$", s)
if false == b {
return b
}
}
return b
}
//数字+字母+符号 6~30位
func IsPwd(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^[0-9a-zA-Z@.]{6,30}$", s)
if false == b {
return b
}
}
return b
}
/************************* 数字类型 ************************/
//纯整数
func IsInteger(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^[0-9]+$", s)
if false == b {
return b
}
}
return b
}
//纯小数
func IsDecimals(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^\\d+\\.[0-9]+$", s)
if false == b {
return b
}
}
return b
}
//手提电话不带前缀最高11位
func IsCellphone(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^1[0-9]{10}$", s)
if false == b {
return b
}
}
return b
}
//家用电话(不带前缀) 最高8位
func IsTelephone(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^[0-9]{8}$", s)
if false == b {
return b
}
}
return b
}
/************************* 英文类型 *************************/
//仅小写
func IsEngishLowCase(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^[a-z]+$", s)
if false == b {
return b
}
}
return b
}
//仅大写
func IsEnglishCap(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^[A-Z]+$", s)
if false == b {
return b
}
}
return b
}
//大小写混合
func IsEnglish(str ...string) bool {
var b bool
for _, s := range str {
b, _ = regexp.MatchString("^[A-Za-z]+$", s)
if false == b {
return b
}
}
return b
}
//邮箱 最高30位
func IsEmail(s string) (bool, error) {
return regexp.MatchString("^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$", s)
}