Article: ASP Input Validation

ASP Input Validation

Article:
ASP Input Validation

Posted by: Carlos on 16/06/2008 21:35:42
Star Rating of 1. Actual Rating of: 4
 You must be logged in to vote on thread ratings
Data integrity is one of those tedious, horrible tasks no one really wants to do. After all, when you have a form with 20 fields worth of data coming in from a form post who really wants to check that the email address is properly formatted, or that a telephone doesnt contain any letters or symbols!?
Data integrity is one of those tedious, horrible tasks no one really wants to do. After all, when you have a form with 20 fields worth of data coming in from a form post who really wants to check that the email address is properly formatted, or that a telephone doesnt contain any letters or symbols!? Standard methods might have you checking its length isn't 0, or that its a Cint data type, or that it has an @ symbol in the string etc etc...

A quicker, and more manageable option is to stick all these checks into a function and simply call the function with paramaters based on what you're checking for. For example:

submitEmail=False
submitTelephone=False
emailData=request.form("email")
< i>telephoneData=request.form("tel")
if validation_check(emailData,"Email",null,null) then submitEmail=True
if validation_check(telephoneData,"Tel",null,null) then submitTelephone=True

The function that we call our checks against can be found below. It consists of nothing more than a series of if statements that checks against what type of validation you're wanting, and then runs the subsequent regular expression code. We wont go into the ins and outs of regular expressions here, as theyre a rather complex beast and to be honest....you can usually manage without knowing exactly how it works. If you need a regular expression check for a different data format, simply have a quick search on google and you're sure to find what you're after! The function we often use in many of our sites below covers all the usual data types that we check against. If the regular expression pattern is found the function returns True, otherwise it returns False.

Lets take a look at our function: 

Function validation_checks(inputStr, regtype, min, max)
    set reg=new RegExp
    reg.Global=True
    reg.Ignorecase=True
    if regtype="chat" then
        reg.Pattern="\S+?"
    elseif regtype="numerical" then
        reg.Pattern="^\d{"&min&","&max&"}$"
    elseif regtype="alphanumeric" then
        reg.pattern="^[\w ]{"&min&","&max&"}$"
    elseif regtype="usernameorpassword" then
        reg.pattern="^[\w _-]{"&min&","&max&"}$"
    elseif regtype="alpha" then
        reg.pattern="^[a-zA-Z ]{"&min&","&max&"}$"
    elseif regtype="sentence" then
        reg.pattern="^[\w\.\(\)\\\*\\+\?\$,'-_&%""!= ]{"&min&","&max&"}$"
    elseif regtype="email" then
        reg.pattern="^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$"
  &n bsp; elseif regtype="emailornone" then
        reg.pattern="^([\w-_.]*[\w-_.]@[\w-_]+?[\w-_.]+\.[\w.]{3,})?$"
    elseif regtype="postcode" then
        reg.pattern="^[a-zA-Z]+[a-zA-Z0-9]{1,3}\s[\d]{1}[a-zA-Z]{2}$"
    elseif regtype="phone" then
        reg.pattern="^[\d ]{"&min&","&max&"}$"
    elseif regtype="money" then
        reg.pattern="^[\d]{"&min&","&max&"}\.?[\d]{0,2}$"
    elseif regtype="score" then
        reg.pattern="^[\d]{"&min&","&max&"}\.[\d]{1}$"
    elseif regtype="coordinate" then
        reg.pattern="^[-]?[\d]{0,3}\.?[\d]{0,4}$"
    elseif regtype="dob" then
        reg.pattern="^([\d]{1,2}/[\d]{1,2}/[\d]{4})?$"
    elseif regtype="gamedate" then   
        reg.pattern="\d{2}?/\d{2}?/\d{4}?|TBA|TBA.??\d{4}?"
    end if
    validation_checks=reg.test(inputStr)           
end function

Our function takes 4 input parameters. An inputstring (your form data or querystring etc), regtype (the pattern you want to check against from the if statements), and a min and max (not needed for all of the validation checks, in which case null can be sent). Min and Max are useful where a field may not be required, but if something is entered you want to check it's right...I.E. a telephone number may not be a requirement, but if you enter something it needs to be digits only and no more than 11 digits, so you'd send a min of 0 and a max of 11.

And thats it basically! By calling this function on all your collected data you can quickly verify data input integrity, and better still because its a function you can re-use it over and over again.

Check back for more input validation techniques soon!

Comments board:

There are no comments yet. Be the first to post!
Gr00pz © 2006-2008
         
   Art-Affinity   |    Mac-Affinity   |    Gadget-Affinity   |    PCGamer   |   Rules FAQ Network 
Web,developer,website,forums,community,asp,php,html,css,j4vascript,help