check.go 1.53 KB
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
package utils

import (
	"regexp"
)

/* 非空
 * Check_empty([]string) bool
 * 有空值返回true 无空值返回false
 *
 * 密碼規則
 * Check_authword(authword string) bool 密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线)
 * 通过正则返回true 不通过返回false
 *
 * 郵箱檢查
 * Check_email(email string) bool Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
 * 通过正则返回true 不通过返回false
 *
 * ip檢查
 *  Check_ip(ip string) (err bool)
 *
 */

func Check_empty(strs ...string) bool {
	for i := range strs {
		if strs[i] == "" {
			return true
		}
	}
	return false
}

func Check_ip(ip string) (err bool) {
	pattern := `^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d?)\.((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d?|0)\.){2}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d?)$`
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(ip)
}

func Check_email(emial string) bool {
	pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(emial)
}

func Check_authword(authword string) bool {
	pattern := `^.{8,32}$`
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(authword)
}

func Check_phone(authword string) bool {
	pattern := "^1[345789]{1}\\d{9}$"
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(authword)
}

func CheckIdCard(idCard string) bool {
	pattern := "^([1-6][1-9]|50)\\d{4}(18|19|20)\\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$"
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(idCard)
}