初始化项目
This commit is contained in:
48
trunk/goutil/validationUtil/regexpValidation.go
Normal file
48
trunk/goutil/validationUtil/regexpValidation.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package validationUtil
|
||||
|
||||
import "regexp"
|
||||
|
||||
var (
|
||||
// 身份证号
|
||||
re_idno = regexp.MustCompile("^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$")
|
||||
// 邮箱
|
||||
re_email = regexp.MustCompile(`^[0-9A-Za-z][\.-_0-9A-Za-z]*@[0-9A-Za-z]+(\.[0-9A-Za-z]+)+$`)
|
||||
// 中国手机号验证
|
||||
re_chinesePhoneNum = regexp.MustCompile(`^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\d{8}$`)
|
||||
)
|
||||
|
||||
// 验证身份证号码
|
||||
// idno:身份证号
|
||||
// 返回值:
|
||||
// bool: true:成功 false:不合法
|
||||
func IsValideIdno(idno string) bool {
|
||||
if re_idno.MatchString(idno) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 验证邮箱
|
||||
// email:邮箱
|
||||
// 返回值:
|
||||
// bool: true:成功 false:不合法
|
||||
func IsValideEmail(email string) bool {
|
||||
if re_email.MatchString(email) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 验证中国的手机号码
|
||||
// phoneNum:待验证的手机号码
|
||||
// 返回值:
|
||||
// bool: true:成功 false:不合法
|
||||
func IsValideChinesePhoneNum(phoneNum string) bool {
|
||||
if re_chinesePhoneNum.MatchString(phoneNum) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
62
trunk/goutil/validationUtil/regexpValidation_test.go
Normal file
62
trunk/goutil/validationUtil/regexpValidation_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package validationUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 身份证测试
|
||||
func TestIdCard(t *testing.T) {
|
||||
idno := "450325197410077393"
|
||||
if IsValideIdno(idno) == false {
|
||||
t.Error("身份证验证出错:", idno)
|
||||
t.Fail()
|
||||
}
|
||||
idno = "36062219701120774X"
|
||||
if IsValideIdno(idno) == false {
|
||||
t.Error("身份证验证出错:", idno)
|
||||
t.Fail()
|
||||
}
|
||||
idno = "450325197410071111"
|
||||
if IsValideIdno(idno) == false {
|
||||
t.Error("身份证验证出错:", idno)
|
||||
t.Fail()
|
||||
}
|
||||
idno = "3123123123"
|
||||
if IsValideIdno(idno) == true {
|
||||
t.Error("身份证验证出错:", idno)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// 邮箱测试
|
||||
func TestMail(t *testing.T) {
|
||||
mail := "nihao@qq.com"
|
||||
if IsValideEmail(mail) == false {
|
||||
t.Error("邮箱验证出错:", mail)
|
||||
t.Fail()
|
||||
}
|
||||
mail = "111@qq.com"
|
||||
if IsValideEmail(mail) == false {
|
||||
t.Error("邮箱验证出错:", mail)
|
||||
t.Fail()
|
||||
}
|
||||
mail = "111_@qq.com"
|
||||
if IsValideEmail(mail) == false {
|
||||
t.Error("邮箱验证出错:", mail)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
// 验证中国的手机号
|
||||
func TestChinesePhone(t *testing.T) {
|
||||
phoneNum := "15111111111"
|
||||
if IsValideChinesePhoneNum(phoneNum) == false {
|
||||
t.Error("手机号验证出错:", phoneNum)
|
||||
t.Fail()
|
||||
}
|
||||
phoneNum = "11111"
|
||||
if IsValideChinesePhoneNum(phoneNum) == true {
|
||||
t.Error("手机号验证出错:", phoneNum)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
87
trunk/goutil/validationUtil/validationUtil.go
Normal file
87
trunk/goutil/validationUtil/validationUtil.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package validationUtil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"goutil/stringUtil"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxInt32 int = 0x7fffffff
|
||||
MinInt32 int = -0x7fffffff
|
||||
)
|
||||
|
||||
// 检查数值范围
|
||||
// errList:错误列表
|
||||
// val:待检查的值
|
||||
// min:最小值
|
||||
// max:最大值
|
||||
// msg:错误提示
|
||||
// 返回值:
|
||||
// bool:是否在范围内
|
||||
func CheckIntRange(errList *([]error), val int, min int, max int, msg string) bool {
|
||||
if val < min || val > max {
|
||||
if errList != nil {
|
||||
*errList = append(*errList, errors.New(msg))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查数值范围
|
||||
// errList:错误列表
|
||||
// val:待检查的值
|
||||
// min:最小值
|
||||
// max:最大值
|
||||
// msg:错误提示
|
||||
// 返回值:
|
||||
// bool:是否在范围内
|
||||
func CheckFloatRange(errList *([]error), val float64, min float64, max float64, msg string) bool {
|
||||
if val < min || val > max {
|
||||
if errList != nil {
|
||||
*errList = append(*errList, errors.New(msg))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查字符串是否为空
|
||||
// errList:错误列表
|
||||
// val:待检查的值
|
||||
// msg:错误提示
|
||||
// 返回值:
|
||||
// bool:是否在范围内
|
||||
func Require(errList *([]error), val string, msg string) bool {
|
||||
if stringUtil.IsEmpty(val) {
|
||||
if errList != nil {
|
||||
*errList = append(*errList, errors.New(msg))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查数据是否存在重复项(此函数效率较低,如有效率要求请不要调用此函数)
|
||||
func IsDistinct(count int, isEqualFunc func(i, j int) bool) bool {
|
||||
for i := 0; i < count; i++ {
|
||||
for j := 0; j < count; j++ {
|
||||
if i == j {
|
||||
continue
|
||||
}
|
||||
|
||||
if isEqualFunc(i, j) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user