const ERROR_NULL = " Empty!" const ERROR_EMAIL= " Invalid!" const ERROR_EMAIL_LENGTH = " Too Long!" const ERROR_FORMAT = " Invalid!" const ERROR_DATE = " Invalid!" const ERROR_USERNAME = " Invalid!" const ERROR_USERNAME_EMPTY = " Empty!" const ERROR_PWD_EMPTY = " Empty!" const ERROR_PWD = " Invalid!" const ERROR_PWD_CONFIRM = " Confirm Password!" 'String s 需要验证的字符串 'String msg 返回的出错信息 'bool bRequire 该字段是否为必须 'Public Function IsEmail (ByVal s, ByRef msg, ByVal bRequire) 是否为一个有效的电子信箱格式 'Public Function IsInt(ByVal s, ByVal item , ByRef msg, ByVal bRequire) 是否是一个数值 'Public Function IsMoney(ByVal s, ByRef msg, ByVal bRequire) 是否为一个大于等于0的数字 'Public Function IsDateTime(ByVal s , ByRef msg, ByVal bRequire) 时间日期的格式(mm/yy)有效 'Public Function CheckEmpty(ByVal s , ByRef msg) 为空则返回false 'Public Function IsUsername (ByVal s , ByRef msg) 用户名的有效 'Public Function IsPassword (Byval s1 , Byval s2 , ByRef msg) 密码的有效性 ' 检查s是否在字符集bag中 Public Function IsCharsInBag (ByVal s,ByVal bag) s = Trim(s) bag = Trim (bag) Dim i,c ' Search through string's characters one by one. ' If character Is in bag, append to returnString. For i = 1 to Len(s) c = Mid(s,i,1) If Instr(bag,c) = 0 Then IsCharsInBag = c Exit Function End If Next IsCharsInBag = "" End Function '检查是否为空 '空: false 非空:true Public Function CheckEmpty(ByVal s , ByRef msg) If s = "" or len(s)=0 Then msg = ERROR_NULL CheckEmpty = false Exit Function End If If Trim(s) = "" Then msg = ERROR_NULL CheckEmpty = false Exit Function Else msg = "" CheckEmpty = true Exit Function End If End Function '检查EMAIL '合法:true 非法:false Public Function IsEmail (ByVal s, ByRef msg, ByVal bRequire) s = Trim(s) ' there must be >= 1 character befOre @, so we ' start looking at character position 2 ' (i.e. second character) If bRequire = true Then If CheckEmpty(s, msg) = false Then IsEmail = false Exit Function End If Else If Len(s) = 0 Then msg = "" IsEmail = true Exit Function End If End If Dim i Dim s_len s_len = Len(s) If s_len > 50 Then msg = ERROR_EMAIL_LENGTH IsEmail = false Exit Function End If Dim pos1 Dim pos2 Dim pos3 Dim pos4 pos1 = Instr(s,"@") pos2 = Instr(s,".") pos3 = InstrRev(s,"@") pos4 = InstrRev(s,".") 'check '@' And '.' Is not first Or last character If pos1 <= 1 Or pos1 = s_len Or pos2 <=1 Or pos2 = s_len Then msg = ERROR_EMAIL IsEmail = false Exit Function Else 'check @. Or .@ If pos1 = pos2 - 1 Or pos1 = pos2 + 1 Or pos1 <> pos3 Or pos4 < pos3 Then msg = ERROR_EMAIL IsEmail = false Exit Function End If End If If IsCharsInBag( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@") <> "" Then msg = ERROR_EMAIL IsEmail = false Exit Function End If msg = "" IsEmail = true End Function Public Function IsInt(ByVal s, ByRef msg, ByVal bRequire) s = Trim(s) If bRequire = true Then If CheckEmpty(s, msg) = false Then IsInt = false Exit Function End If Else If Len(s) = 0 Then msg = "" IsInt = true Exit Function End If End If If not s = "" Then If IsNumeric(s) Then ' And CLng(s) - s = 0 And CLng(s) > 0 msg = "" IsInt = true Exit Function Else msg = ERROR_FORMAT IsInt = false Exit Function End If Else msg = "" IsInt = true Exit Function End If End Function Public Function IsTinyInt(ByVal s, ByRef msg, ByVal bRequire) s = Trim(s) If IsInt(s, msg, true) = true Then If int(s) >= 0 and int(s) <= 255 Then msg = "" IsTinyInt = true Exit Function Else msg = ERROR_FORMAT IsTinyInt = false Exit Function End If Else msg = ERROR_FORMAT IsTinyInt = false Exit Function End If End Function Public Function IsMoney(ByVal s, ByRef msg, ByVal bRequire) s = Trim(s) If bRequire = true Then If CheckEmpty(s, msg) = false Then IsMoney = false Exit Function End If Else If Len(s) = 0 Then msg = "" IsMoney = true Exit Function End If End If If not s = "" Then If IsNumeric(s) Then If s < 0 Then msg = ERROR_FORMAT IsMoney = false Exit Function Else msg = "" IsMoney = true Exit Function End If Else msg = ERROR_FORMAT IsMoney = False Exit Function End If End If End Function '判断是否为日期型的函数 Public Function IsDateTime(ByVal s , ByRef msg, ByVal bRequire) s = Trim(s) If bRequire = true Then If CheckEmpty(s, msg) = false Then IsDateTime = false Exit Function End If Else If Len(s) = 0 Then msg = "" IsDateTime = true Exit Function End If End If dim mm , yy dim pos1 , pos2 pos1 = Instr(s,"/") pos2 = InstrRev(s,"/") If not pos1 = pos2 Then '两个间隔符,出错 msg = ERROR_DATE IsDateTime = false Exit Function End If If pos1 = 0 Then msg = ERROR_DATE IsDateTime = false Exit Function End If mm = Left(s,pos1-1) yy = Right(s,Len(s)-pos1) If not isNumeric(mm) Then msg = ERROR_DATE IsDateTime = false Exit Function End If If not isNumeric(yy) Then msg = ERROR_DATE IsDateTime = false Exit Function End If If CLng(mm) >12 or CLng(mm) =< 0 Then msg = ERROR_DATE IsDateTime = false Exit Function End If If Len(yy) = 4 Then '四位数的年份 If CLng(yy) >= 2000 and CLng(yy) < 2010 Then msg = "" IsDateTime = true Exit Function Else '年份的数字出错 msg = ERROR_DATE IsDateTime = false Exit Function End If Else If Len(yy) = 2 Then If CLng(yy) >0 and CLng(yy) <100 Then msg = "" IsDateTime = true Exit Function Else msg = ERROR_DATE IsDateTime = false Exit Function End If End If End If msg = "" IsDateTime = true End Function Public Function IsUsername (ByVal s , ByRef msg) Dim c If CheckEmpty( s , msg ) = false Then msg = ERROR_USERNAME_EMPTY IsUsername = false Exit Function End If c = IsCharsInBag( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@") If c <> "" Then msg = ERROR_USERNAME '& "'" & c & "' !" IsUsername = false Exit Function End If msg = "" IsUsername = true End Function Public Function IsPassword (Byval s1 , Byval s2 , ByRef msg) Dim c If CheckEmpty( s1 , msg ) = false Then msg = ERROR_PWD_EMPTY IsPassword = false Exit Function End If If not s1 = s2 Then msg = ERROR_PWD_CONFIRM IsPassword = false Exit Function End If c = IsCharsInBag( s1, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@") If c <> "" Then msg = ERROR_PWD '& "'" & c & "' !" IsPassword = false Exit Function End If msg = "" IsPassword = true End Function