ASP 表单验证方法总结
发布时间:2022-10-21 15:10:50 所属栏目:Asp教程 来源:
导读: ASP表单验证方法总结
虽然说表单验证在客户端使用javascript来验证已经可以了,但是我们为了防止访客绕过客户端验证也为了数据安全最好还是在服务器端加上必要的验证,下面我们以实例来讲述asp表单验证方
虽然说表单验证在客户端使用javascript来验证已经可以了,但是我们为了防止访客绕过客户端验证也为了数据安全最好还是在服务器端加上必要的验证,下面我们以实例来讲述asp表单验证方
|
ASP表单验证方法总结 虽然说表单验证在客户端使用javascript来验证已经可以了,但是我们为了防止访客绕过客户端验证也为了数据安全最好还是在服务器端加上必要的验证,下面我们以实例来讲述asp表单验证方法。 1、验证输入的数字 假设一个文本框 <form method="post" action=""> <input type="text"> </form>要求用户必须输入数字 if not isnumeric(Request.Form("textfield")) then response.write "重新填写" end if要求限制数字长度,如你要用户输入oicq号码 此例限制了用户的输入只有为4到10位数字才有效 if len(Request.Form("textfield"))>10 or len(Request.Form("textfield"))<4 then response.write "重新填写" end if当然上面用Request.Form和Request是一样的ASP表单,随便你怎么写了。 2、验证用户输入的邮件地址 引用一段通用检测函数来说明 由于检验程序较长,将其定义为一函数来调用 function IsValidEmail(email) dim names, name, i, c ’Check for valid syntax in an email address. IsValidEmail = true names = Split(email, "@") if UBound(names) <> 1 then IsValidEmail = false exit function end if for each name in names if Len(name) <= 0 then IsValidEmail = false exit function end if for i = 1 to Len(name) c = Lcase(Mid(name, i, 1)) if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then IsValidEmail = false exit function end if next if Left(name, 1) = "." or Right(name, 1) = "." then IsValidEmail = false exit function end if next if InStr(names(1), ".") <= 0 then IsValidEmail = false exit function end if i = Len(names(1)) - InStrRev(names(1), ".") if i <> 2 and i <> 3 then IsValidEmail = false exit function end if if InStr(email, "..") > 0 then IsValidEmail = false end if end function上面的这段函数大家应该都看的懂,当然你可以修改这段代码,使得即使用户输入XXX@CCC.DDD是错误的邮件地址,因为DDD不是一个有效域名。 引用的时候可以这样写 if IsValidEmail(trim(request("textfield")))=false then response.write "重新填写" end if 3、验证为空的表单单元 有的信息是要求用户必须填写的,所以不允许为空,因此当用户输入为空的时候需要提示。 对为空单元的处理 if Request.Form("textfield")="" then Response.write "填写为空" end if 4、判断用户输入的是不是一个日期 首先明白日期值格式2002-11-19 (编辑:航空爱好网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐

