<%
'***************************************************************
' 文件名: validate.asp
' 描述: 表单验证类
' 作者: Stone <1182140@qq.com>
' 版本: 0.1 Beta <2008 年 12 月 19 日>
'***************************************************************
Class Validator
Private objRegExp
Private blnRemoteSubmit
Private ErrorMessage
Private intValidateCount
Private arrValidate()
Private Sub Class_Initialize()
ErrorMessage = ""
intValidateCount = 0
blnRemoteSubmit = False
End Sub
Public Property Get Err()
Err = ErrorMessage
End Property
Public Sub Add(Element, RegType, Para, ErrMsg)
Redim Preserve arrValidate(3,intValidateCount)
arrValidate(0,intValidateCount) = Element
arrValidate(1,intValidateCount) = RegType
arrValidate(2,intValidateCount) = Para
arrValidate(3,intValidateCount) = ErrMsg
intValidateCount = intValidateCount + 1
End Sub
Public Function Validate
If Not blnRemoteSubmit Then
Dim strHttpReferer, strServerName
strHttpReferer = CStr(Request.ServerVariables("HTTP_REFERER"))
strServerName = CStr(Request.ServerVariables("SERVER_NAME"))
If InStr(strHttpReferer, strServerName) <> 8 Then
ErrorMessage = "程序不允许站外提交"
Validate = False
Exit Function
End If
End If
Dim i
For i = 0 To intValidateCount - 1
Select Case arrValidate(1, i)
Case "d"
Check CheckDate(arrValidate(0, i), arrValidate(2, i)), arrValidate(3, i)
Case "s"
Check CheckString(arrValidate(0, i), arrValidate(2, i)), arrValidate(3, i)
Case "n"
Check CheckNumber(arrValidate(0, i), arrValidate(2, i)), arrValidate(3, i)
Case "k"
Check CheckSelect(arrValidate(0, i)), arrValidate(3, i)
Case Else
Check CheckRegExp(arrValidate(0, i), arrValidate(1, i)), arrValidate(3, i)
End Select
Next
If ErrorMessage = "" Then
Validate = True
Else
Validate = False
End If
End Function
Private Sub Check(blnFunction, strErrMsg)
If Not blnFunction Then
ErrorMessage = ErrorMessage & strErrMsg &"<br>"
End If
End Sub
Private Function CheckString(Element, strPattern)
CheckString = True
If isN(Element) Then
CheckString = False
exit function
end if
Dim lens : lens = IfThen(instr(strPattern,",")>0,split(strPattern,","),array(strPattern,strPattern))
if not lensCheck(Element,lens(0),lens(1)) then CheckString = False
End Function
Private Function CheckNumber(Element, strPattern)
CheckNumber = True
If not IsNumber(Element) Then
CheckNumber = False
exit function
end if
Dim lens : lens = IfThen(instr(strPattern,",")>0,split(strPattern,","),array(strPattern,strPattern))
if not sizeCheck(Element,lens(0),lens(1)) then CheckNumber = False
End Function
Private Function CheckDate(Element, strPattern)
CheckDate = True
If not isDate(Element) Then
CheckDate = False
exit function
end if
Dim lens : lens = IfThen(instr(strPattern,",")>0,split(strPattern,","),array(strPattern,strPattern))
if not dateCheck(Element,lens(0),lens(1)) then CheckDate = False
End Function
Private Function CheckSelect(Element)
CheckSelect = True
If isN(Element) Then
CheckSelect = False
end if
End Function
Private Function CheckRegExp(Element, RegType)
Set objRegExp = New RegExp
With objRegExp
.Global = False
.IgnoreCase = True
End With
objRegExp.Pattern = RegType
CheckRegExp = Eval(objRegExp.Test(Element))
End Function
Public Function IfThen(C,T,F)
If C Then IfThen = T Else IfThen = F End If
End Function
Public Function isN(str)
isN = False
If IsArray(str) Then
Exit Function
End If
If str = "" or IsEmpty(str) or IsNull(str) Then isN = True
End Function
Public Function IsNumber(Str)
IsNumber = false
If Not IsN(Str) And isNumeric(Str) Then IsNumber = true
End Function
Public Function lensCheck(Str,n1,n2)
lensCheck = True
if len(Str)<cint(n1) or len(Str)>cint(n2) then lensCheck = False
End Function
Public Function sizeCheck(Str,n1,n2)
sizeCheck = True
if Str*100<n1*100 or Str*100>n2*100 then sizeCheck = False
End Function
Public Function dateCheck(date,n1,n2)
dateCheck = True
if DateDiff("d",n1,date)<0 or DateDiff("d",n2,date)>0 then dateCheck = False
End Function
End Class
dim action:action=request.querystring("action")
if action="save" then
Dim objValidator : Set objValidator = New Validator
With objValidator
.Add request.form("username"), "s","4,14", "用户名称不正确"
.Add request.form("password"), "s","6,14" ,"密码不正确"
.Add request.form("age"), "n","20,99","年龄不正确"
.Add request.form("date"), "d","2008-1-1,2008-12-1","日期不正确"
.Add request.form("OS"), "k","","操作系统没有选择"
.Add request.form("Province"), "k","","省份没有选择"
.Add request.form("Favorite"), "k","","爱好没有选择"
.Add request.form("postcode"), "^[1-9]/d{5}$","","邮编不正确"
.Add request.form("qq"), "^[1-9]/d{4,9}$","","QQ不正确"
End With
If Not objValidator.Validate Then
Response.Write(objValidator.Err)
Set objValidator = Nothing
response.end
end if
end if
%>
<html>
<title>我的表单验证类</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<body>
<form name="theForm" id="demo" action="?action=save" method="post" >
<table align="center">
<tr>
<td>用户名:</td><td><input name="userName" ></td>
</tr>
<tr>
<td>密码:</td><td><input name="Password" ></td>
</tr>
<tr>
<td>日期:</td><td><input name="date" ></td>
</tr>
<tr>
<tr>
<td>年龄:</td><td><input name="age" ></td>
</tr>
<tr>
<td>邮编:</td><td><input name="postcode" ></td>
</tr>
<tr>
<td>QQ:</td><td><input name="QQ" ></td>
</tr>
<tr>
<td>操作系统:</td><td><select name="OS" ><option value="">选择您所用的操作系统</option><option value="Win98">Win98</option><option value="Win2k">Win2k</option><option value="WinXP">WinXP</option></select></td>
</tr>
<tr>
<td>所在省份:</td><td>广东<input name="Province" value="1" type="radio">陕西<input name="Province" value="2" type="radio">浙江<input name="Province" value="3" type="radio">江西<input name="Province" value="4" type="radio" ></td>
</tr>
<tr>
<td>爱好:</td><td>运动<input name="Favorite" value="1" type="checkbox">上网<input name="Favorite" value="2" type="checkbox">听音乐<input name="Favorite" value="3" type="checkbox">看书<input name="Favorite" value="4" type="checkbox" ></td>
</tr>
<tr>
<td colspan="2"><input name="Submit" type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>