ASP伺服器端表單驗證類
<%
'***************************************************************
' 檔名: 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>
相關文章
- 表單驗證工具類ValidationUtils
- JavaScript 表單及表單驗證JavaScript
- bootstrap表單驗證boot
- javascript表單驗證JavaScript
- Laravel 表單驗證Laravel
- JavaScript 表單驗證JavaScript
- Django表單驗證Django
- jquery 表單驗證jQuery
- Js表單驗證JS
- Asp.net MVC中表單驗證ASP.NETMVC
- 使用 Laravel 請求類來驗證表單請求Laravel
- 用ASP動態生成JavaScript的表單驗證程式碼 (轉)JavaScript
- 表單required 必需驗證UI
- 表單資料驗證
- HTML 表單驗證概述HTML
- bootstrapValidator 表單驗證boot
- 表單驗證——筆記筆記
- angularjs表單驗證AngularJS
- JavaScript表單驗證事件JavaScript事件
- ElementUi rules表單驗證UI
- 表單驗證<AngularJs>AngularJS
- Web 表單驗證 javascriptWebJavaScript
- 輸入表單驗證
- Lumen 仿 Laravel 表單驗證Laravel
- 前端表單驗證的目的前端
- 表單驗證教程簡介
- Layui 自定義表單驗證UI
- vue表單驗證你真的會了嗎?元件之表單驗證(form)validateVue元件ORM
- iview表單驗證問題 Select驗證必填失敗,以及表單物件巢狀陣列驗證方法View物件巢狀陣列
- elementui表單驗證 對比兩個表單大小UI
- 走進AngularJs(九)表單及表單驗證AngularJS
- Laravel 自定義表單驗證-自定義驗證規則Laravel
- ASP.NET學習手記:驗證使用者表單輸入 (轉)ASP.NET
- Angular 表單驗證類庫 ngx-validator 1.0 正式釋出Angular
- 表單驗證使用擴充套件套件
- 純CSS實現表單驗證CSS
- jQuery表單驗證效果詳解jQuery
- jquery.validate 表單驗證jQuery