When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Wednesday, December 01, 1999

Server-Side Form Validation
By Dianna Leech


Users are irritating. They don't like filling out forms, and will tear through them as fast as they possibly can to get to the fun part of your site. Since they are typing so fast, they probably won't read the directions you so kindly put at the top of the form, and will put in who knows what kind of garbage just to send the thing off and get to the next page.

- continued -

Since this data is usually important to us web developers, we'd like to have it be as accurate as possible before it's added it to our database. So, we need to do validation on the data before submitting it. There are two places where this checking can occur: on the client or on the server. Client-side form validation is done with a JavaScript function which fires when the user clicks the submit button. If there are errors, the user stays on that page to fix them.

Client-side validation has a few things in its favor. It's quick, the user knows immediately that something is wrong, and it's pretty easy to code. (Always an important consideration, for me anyway.) However it does have a major flaw. It's not secure. At all. Anybody viewing your web page can do View>>Source and look at your validation script. This will tell them what's being checked in your form and what's not. A malicious person, or one who's just bored, can save your page to their computer, edit or delete your validation code and then submit your form. Repeatedly. It's not hard to see that a security hole that big should be patched.

The way to avoid this problem is to do server-side validation. To implement server-side validation, we write an ASP script that handles the validation and save it as another ASP page. We put the name of this ASP in the Action= part of the form on the first page. When the user submits the form, the validation script page is run. If the user fails the validation routine we kick him back to the form page where we'll print out a table of his errors, so he knows exactly what to fix.

Before going any further, let me confess up front that this script uses session variables. I agree with others on this site who've said that session variables are evil, but they are convenient little devils. So just remember, session variables are a type of temporary cookie and if your users surf with cookies turned off this script won't work.

Enough description, lets see the code!

On our form page, first we create an HTML table which align our form correctly.

<HTML><HEAD></HEAD>
<BODY>
<TABLE border=0 width=500 >
<TR><TD>
<%
'ON THE FIRST TIME THAT THIS PAGE LOADS, SESSION("ERRORS") HAS NO VALUE AND SO 
'EQUALS 0.  ON SUBSEQUENT VISITS, SESSION("ERRORS WILL HAVE A VALUE OF 1 OR 
'MORE IF ERRORS WERE MADE  

if  Session("Errors")=0 then 
  response.write "Please fill out the form"
else
  'ERRORS WERE MADE SO LIST THEM IN THE REST OF THE TABLE
  'reset our error counter
  Session("Errors")="0"
  response.write "<BR>There are errors in your data.  " & _
          "Please make  the following  changes before " & _
          "clicking the submit button:<br>"
  response.write"<TABLE border=0 width=' 400' align='center'>"

  'THESE SESSION VARIABLES ARE SET IN THE SECOND PAGE
  ' IF THERE WERE ERRORS 'VALUE IS "F"  
  If Session("badFirstName") = "T" then 
     Response.write "<TR><TD><font color='red'>The First " & _
                    "Name field must be completed.</font>" 
     Response.write "</TD></TR>"
     Session("badFirstName")="F"
  End If 


  If Session("badLastName") = "T" then
    Response.write "<TR><TD><font color='red'>The Last Name " & _
                   "field must be completed. </font>" 
    Response.write "</TD></TR>"
    Session("badLastName")="F"
  End If 

  If Session("badDate") = "T" then
    Response.write "<TR><TD><font color='red'>Date must be " & _
                   "in the format mm/dd/yyyy. </font>" 
    Response.write "</TD></TR>"
    Session("badDate")="F"
  End If 

  'END THE ERRORS TABLE
  response.write "</TD></TR></TABLE>"
End If
%>	

<!--START THE FORM -->
<FORM ACTION="test.asp" NAME="frmUser" METHOD="POST">

<!--FORM IS IN A TABLE TO ALIGN THE TEXTBOXES -->
<TABLE ALIGN="" BORDER=0 ALIGN="">
<TR>
<TD align=right><B>First Name</B></TD>
<TD>
  <INPUT TYPE="text" Name="FirstName" VALUE="<%=Session("FName")%>">
</TD>
</TR>

<TD align=right><B>Last Name</B></TD>
<TD>
  <INPUT TYPE="text" Name="LastName" VALUE="<%=Session("LName")%>">
</TD>
</TR>

<TR><TD align="right">
  <B>Date </B></td><TD><INPUT TYPE="Text" NAME="SendDate" 
             VALUE="<%=Session("CompletionDate") %>"> 
             <font color="blue">(mm/dd/yyyy)</font>
</TD></TR>
</TABLE>
<!-- end table for data entry form -->

<INPUT TYPE="Submit" VALUE="Submit">  <INPUT TYPE="RESET">
</FORM>

</TD></TR></TABLE>
<!-- end table used for page alignment -->
</BODY>
</HTML>

OK, now the front form page is done, on to the validation code on the second page, test.asp!

<% Session("FName")=Request("FirstName") Session("LName")=Request("LastName") Session("CompletionDate")=Request("SendDate") 'HERE IS ONE WAY OF CHECKING FOR AN EMPTY TEXT BOX if not len(Request("FirstName")) > 0 then Session("badFirstName")="T" Session("Errors")=Session("Errors") + 1 end if 'AND HERE IS ANOTHER, BOTH SHOULD WORK if Request("LastName")= "" then Session("badLastName")="T" Session("Errors")=Session("Errors") + 1 end if if not IsDate(Request("SendDate")) then Session("badDate")="T" Session("Errors")=Session("Errors") + 1 end if if Session("Errors") > 0 then ‘there were errors, so send back to form response.redirect "form.asp" else 'there were no errors, so do the update to the database and redirect to a thank you page response.redirect "thanks.asp" end if %>

That's all there is to it! Happy Programming!


Attachments

  • Download the source code for the form creation web page in text format
  • Download the source code for the form field error checking script in text format


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES