数据验证之类型与格式

来源:互联网 发布:lrtimelapse windows 编辑:程序博客网 时间:2024/04/29 09:27

    我一向给予用户最大的自由和最严格的限制。这两点并不矛盾,对于用户的输入,能不做限制的地方尽量不做限制,但是一旦做出限制,就要进行严格的验证。比如在允许用户输入字符的地方,我会允许用户输入任何字符;在要求用户输入日期的地方,则对日期格式进行验证。
    对于接收到的数据,安全的做法是验证其是否为空,长度及数据类型是否符合要求(与数据库字段定义是否一致),格式是否符合要求,甚至需要验证其来源。这样做,不仅是为了避免服务器返回错误,还可以避免遭受功击。
    让我们看一下攻击者是怎么做的吧。以SQL注入为例,假如你有一个页面是list.asp?id=1,攻击者做的第一步是在这个URL后面加上and 1=1,如果你对接收到的id的值没有做数据类型的验证,攻击者会得到一个正常的页面,这就等于告诉他们,他们终究会得到他们想要的东西。
    下面给出几个我常用的用于验证数据类型及格式的自定义函数

Rem 验证自然数
function CheckNar(thisnar)
   dim patrn,matc
   patrn = "^[1-9]{1}(/d)*$"
   matc = CheckExp(patrn,thisnar)
   CheckNar = matc
end function

Rem 验证电子邮件地址
function CheckMail(thismail)
   dim patrn,matc
   patrn = "^((/w)+[.-_]?(/w)+)+[@]{1}(/w)+([.]{1}(/w)+){1,3}$"
   matc = CheckExp(patrn,thismail)
   CheckMail = matc
end function

Rem 验证电话号码
function CheckTel(thistel)
   dim patrn,matc
   patrn = "^([0]{1}(/d)+[-]{1})?([1-9]{1}(/d)+[-]{1})?[1-9]{1}(/d)*$"
   matc = CheckExp(patrn,thistel)
   CheckTel = matc
end function

Rem 验证邮编
function CheckZip(thiszip)
   dim patrn,matc
   patrn = "^[0-9]{6}$"
   matc = CheckExp(patrn,thiszip)
   CheckZip = matc
end function

Rem 验证日期
function CheckDate(thisdate)
   dim patrn,matc,theyear,themonth,theday,thisdate_s
   patrn = "^(19|20){1}(/d){2}[-]{1}[01]?(/d){1}[-]{1}[0-3]?(/d){1}$"
   matc = CheckExp(patrn,thisdate)
   if matc = true then
      thisdate_s = split(thisdate,"-")
      theyear  = cint(thisdate_s(0))
      themonth = cint(thisdate_s(1))
      theday   = cint(thisdate_s(2))
      if theday = 0 then matc = false
      if themonth > 12 or themonth=0 then matc = false
      if themonth = 2 then
         '闰年
         if (theyear mod 4 = 0) and (theyear mod 100 <> 0) or (theyear mod 400 = 0) then
            if theday > 29 then matc = false
         else
            if theday > 28 then matc = false
         end if
      end if
      if themonth = 4 or themonth = 6 or themonth = 9 or themonth = 11 then
         if theday > 30 then matc = false
      else
         if theday > 31 then matc = false
      end if
   end if
   CheckDate = matc
end function

Rem 正则表达式
function CheckExp(patrn,Strng)
   dim re,matchs
   Set re=new RegExp
   re.IgnoreCase =true
   re.Global=True
   re.Pattern = patrn
   matchs = re.Test(Strng)
   CheckExp = matchs 
   set re=Nothing
end function

    以上自定义函数全部利用了正则表达式。
    电子邮件地址,允许在邮件名(@以前的部分)使用.、-、_三个字符。
    电话号码,只允许输入数字及连字符"-",对于010-88888888-8或88888888或13888888888等格式返回True
    对于日期的验证,原本我们可以使用ASP内置的CData()函数:

    function checkDate(dt)
       on error resume next
       tmp = cdate(dt)
       if err.number <> 0 then
          checkDate = false
       else
          checkDate = true
       end if
    end function

    对于2005-1-1,05-1-1,05/1/1,2000-2-29返回True,对于2005-13-1,2005-2-29返回False,看上去似乎没有问题,但是对于05/2/29,也返回True,因为函数误认为我们要验证的是1929-2-5或2029-2-5,于是,我只好自己写一个函数了。该函数只可验证1900-2099之间的日期,通用性有一定限制,而且只允许以类似2005-7-15的格式输入日期,不过,它很准确。

    [本文仅发表于www.netop.cc,blog.csdn.net/netops,netops.blogchina.com 若见于其它各处,皆为转载]