网络编程
位置:首页>> 网络编程>> Asp编程>> ASP表单验证方法总结

ASP表单验证方法总结

  发布时间:2007-10-06 22:43:00 

标签:表单,验证,asp

虽然说表单验证在客户端使用javascript来验证已经可以了,但是我们为了防止访客绕过客户端验证也为了数据安全最好还是在服务器端加上必要的验证,下面我们以实例来讲述asp表单验证方法。

1、验证输入的数字

  假设一个文本框


<form name="form1" method="post" action=""> 
<input type="text" name="textfield"> 
</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是一样的,随便你怎么写了。

 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


0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com