我正在尝试使用下面所示的函数/代码在经典 ASP 中添加一些输入验证。 唯一看起来工作正常的是“文本”类型。其他的我不断收到错误,或者它只是没有正确过滤。 我试图了解我做错了什么,请帮助我。
有效数据类型:“电子邮件”、“整数”、“日期”、“字符串”和“文本”。 前三个很明显,后两个略有差异。
“电子邮件”应仅允许数字和字母以及以下字符“@”、“-”、“>.”、“_”
“日期”应通过运行 IsDate 进行验证,如果为 True,则允许,如果为 False,则不允许。
“字符串”应验证基于文本的查询字符串,仅允许字母、数字、_、- 和 。
而“文本”是任何自由格式的文本表单字段类型内容。
“整数”应该只允许数字和句点 (.)
使用示例: <input type="text" value="<%=MakeSafe("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aedacbdddaeedacbddda80cdc1c3" rel="noreferrer noopener nofollow">[email protected]</a></HTML>1234.5",integer,50)%>">
例如: MakeSafe(dataInput,dataType,dataLength)
<%
'// CODE BY: dB Masters
'// FOUND AT: http://successontheweb.blogspot.com/2008/03/input-validation-for-security-in.html
Function MakeSafeConvert(encodeData)
encodeData = replace(encodeData,"&", "&")
encodeData = replace(encodeData,"'", "'")
encodeData = replace(encodeData,"""", """)
encodeData = replace(encodeData,">", ">")
encodeData = replace(encodeData,"<", "<")
encodeData = replace(encodeData,")", ")")
encodeData = replace(encodeData,"(", "(")
encodeData = replace(encodeData,"]", "]")
encodeData = replace(encodeData,"[", "[")
encodeData = replace(encodeData,"}", "}")
encodeData = replace(encodeData,"{", "{")
encodeData = replace(encodeData,"--", "--")
encodeData = replace(encodeData,"=", "=")
MakeSafeConvert = encodeData
End Function
Function MakeSafe(dataInput,dataType,dataLength)
Dim regex, validInput, expressionmatch
regex = ""
validInput = "1"
If dataType = "string" And Len(dataInput) > 0 Then
regex = "^[\w-\.]{1,"& dataLength &"}$"
ElseIf dataType = "email" And Len(dataInput) > 0 Then
regex = "^[\w-\.]+@([\w-]+\.)+[\w-]{2,6}$"
ElseIf dataType = "integer" And Len(dataInput) > 0 Then
regex = "^\d{1,"& dataLength &"}$"
ElseIf dataType = "date" And Len(dataInput) > 0 Then
If Not IsDate(dataInput) Then validInput = "0" End If
ElseIf dataType = "text" And Len(dataInput) > 0 Then
If Len(dataInput) > dataLength Then validInput = "0" End If
End If
If Len(regex) > 0 And Len(dataInput) > 0 Then
Set RegExpObj = New RegExp
RegExpObj.Pattern = regex
RegExpObj.IgnoreCase = True
RegExpObj.Global = True
RegExpChk = RegExpObj.Test(dataInput)
If Not RegExpChk Then
validInput = "0"
End If
Set RegExpObj = nothing
End If
If validInput = "1" And Len(dataInput) > 0 Then
MakeSafe = MakeSafeConvert(dataInput)
ElseIf Len(dataInput) = 0 Then
MakeSafe = ""
Else
Response.Write "<h2>Processing Halted.</h2>"
Response.End
End If
End Function
%>
示例代码和错误:
当我使用代码测试它时:
<%=MakeSafe("[email protected] ",电子邮件,50)%>
* 不验证任何内容。*
我没有收到错误消息,但它没有验证任何内容。
**输出是:[email protected]/27/20121234.5
应该只是:[email protected] **
当我使用代码测试它时:
<%=MakeSafe("[email protected]/27/20121234.5",日期,50)%>
我没有收到错误消息,但它没有验证任何内容。
输出是:[email protected]/27/20121234.5 应该只是:1/27/2012
另外两个给我这个错误消息:
<%=MakeSafe("[email protected] ",string,50)%>
* 错误!!!参数数量错误或属性分配无效:“string”
<%=MakeSafe("[email protected] ",整数,50)%>
* 错误!!!语法错误
非常感谢您提供的任何帮助...
最佳答案
如果不是拼写错误,那么您的错误就出在函数调用的第二个参数中。
您可以这样调用该函数:
<%=MakeSafe("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ce8f9efe8dce8f9efe8b2fff3f1adaeafa8b2a9" rel="noreferrer noopener nofollow">[email protected]</a>",email,50)%>
这是错误的,因为你也应该“...”第二个参数。这应该有效:
<%=MakeSafe("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="205445535460544553540e434f4d111213140e15" rel="noreferrer noopener nofollow">[email protected]</a>","email",50)%>
关于regex - 使用正则表达式验证 ASP Classic 中的表单和查询字符串。几乎可以工作但缺少一些东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927445/