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: Sunday, February 07, 1999

Server Side Validation Code


This article, by Abd Shomad, is an extension to his Client Side Validation Code. This script allows you to do form validation on the server side. If you've yet to read Abd's article on Client Side Validation, I highly recommend it!

- continued -


<%
'---------------------------------------------------------------------------
' Purpose:  Get Form / Query Item,
' Inputs:
'   sItemName - Name of the form item (string)
'   sAliasName - Name to be appeared on the screen on error occured
'   if sAliasName = "" then use sItemName instead.
'   MinLength, MaxLength: Minimum & Maximum Length of the string
'   respectively
'   bRequired: is the Item
' Returns:
'   The processed value,
'   error message on session("CustomError") and
'   Last entered value at Session(sItemName)
'---------------------------------------------------------------------------

Sub GetFormItem(sItemName, sAliasName, iMinLength, iMaxLength, fRequired) varItem = TRIM(Request.Form(sItemName)) If sAliasName = "" Then sAliasName = sItemName If fRequired Then If varItem = "" Then Session("CustomError") = Session("CustomError") _ & "Please enter at least "&iMinLength&" characters in the <b>""" _ & sAliasName & """</b> field.<br>" Else If Len(varItem) < iMinLength Then Session("CustomError") = _ Session("CustomError") & "Please enter at least " _ & iMinLength &" characters in the <b>""" _ & sAliasName & """</b> field.<br>" If Len(varItem) > iMaxLength Then Session("CustomError") = _ Session("CustomError") & "Please enter at most " _ & iMaxLength &" characters in the <b>""" _ & sAliasName & """</b> field.<br>" End If Else varItem = TRIM(Request.Form(sItemName)) End If Session(sItemName) = varItem End Sub

'---------------------------------------------------------------- ' Purpose: Validate Both Password field, usually used for new registration. ' Inputs : ' sPassword1 : name of the first password input box ' sPassword2 : name of the 2nd password input box ' iMin, iMax: Minimum & Maximum characters allowed respectively ' bRequired : True/False, is required value? ' usually, always TRUE ' Return : ' True/False ' error message on session("CustomError") and '---------------------------------------------------------------- Sub ServerSidePasswordValidation _ (sPassword1, sPassword2, iMin, iMax,bRequired) Call GetFormItem(sPassword1, "Password", iMin, iMax, bRequired) Call GetFormItem(sPassword2, "Verify Password", iMin, iMax, bRequired) bValidPassword = ( session(sPassword1) = session(sPassword2) ) if Not bValidPassword Then Session("CustomError") = _ Session("CustomError") _ & " Both Password field must has the same value!<br>" End Sub

Sub ServerSideEmailValidation _ (sItemName, sAliasName, iMinLength, iMaxLength, bRequired) Call GetFormItem (sItemName, sAliasName, iMinLength, iMaxLength, bRequired) If (InStr(session(sItemName),"@") < 2) Then Session("CustomError") = Session("CustomError") _ & "Not a valid <b>Email</b> address (missing '@')<br>" Else If ( InStr(InStr(session(sItemName),"@"),session(sItemName),".") < _ InStr(session(sItemName),"@")+2 ) Then Session("CustomError") = _ Session("CustomError") _ & "Not a valid <b>Email</b> address (missing '.')<br>" End If End Sub %>

<HTML> <HEAD> <TITLE>Registration</TITLE> </HEAD>

<BODY> <% Call GetFormItem ("name", "Full Name", 3, 50, True) Call GetFormItem ("email", "E-mail", 3, 50, True) Call ServerSidePasswordValidation ("passw1", "passw2", 3, 20, True) Call ServerSideEmailValidation ("email", "E-Mail", 3, 50, True) ' Address is not required field, so we set the bRequired ' (5th Paramenter) to FALSE! See below Call GetFormItem ("address", "Address", 3, 50, False) %>

<P> <FONT COLOR="Red"><STRONG> <% If Session("CustomError") = "" Then Response.Write "Congratulation ... your input has been verified!" Else If session("FirstVisit") = "NO" Then Response.Write _ "Error:<br>" & Session("CustomError") session("FirstVisit") = "NO" End If %> </STRONG></FONT>

<FORM ACTION="" METHOD="POST" onsubmit="return Form_Validator(this)" name="formRegistration"> Full Name:<INPUT TYPE="TEXT" NAME="name" SIZE=20 iMaxLength=50 value=<%= session("name") %>><SUP> *) </SUP><BR> Password : <INPUT TYPE="PASSWORD" NAME="passw1" SIZE=20 iMaxLength=20 value=<%= session("passw1") %>><SUP> *) </SUP><BR> Retype Password: <INPUT TYPE="PASSWORD" NAME="passw2" SIZE=20 iMaxLength=20 value=<%= session("passw2") %>><SUP> *) </SUP><BR> Email : <INPUT TYPE="TEXT" NAME="email" SIZE=20 iMaxLength=50 value=<%= session("email") %>><SUP> *) </SUP><BR> Address <STRONG>(not required field)</STRONG>:<BR> <TEXTAREA NAME="address" ROWS=3 COLS=60><%=session("address")%></TEXTAREA> <BR> <INPUT TYPE="SUBMIT" NAME="s" VALUE=" Submit "> <BR> Note: *) is required field. </FORM>

Your Full Name : <%= session("name") %><BR> Your 1st Password : <%= session("passw1") %><BR> Your Verify Password : <%= session("passw2") %><BR> Your E-mail : <%= session("email") %><BR> Your Address: <%= session("address") %><BR> <BR> Since I save the "requested Item" AND the "error message"<BR> on the session variable, you may use it "everywhere"<BR> on the same application. <BR> Like the above example. <BR> <BR> If Session("CustomError") <> "" Then There are errors on the input fields. <BR> If Session("CustomError") = "" Then proceed the inputs. <BR> <FONT COLOR="Red"><STRONG><% ' Again, i use the Session("CustomError") here If Session("CustomError") <> "" Then Response.Write "There are errors on the input fields." Else Response.Write "No Errors found!. Proceed all the input fields." ' Proceed the input fields here, such as insert the values to ' a new record on some table. End If ' Set Session("CustomError") to "" (NULL STRING) ' If you want to display this custom error on another page, ' Set it's value to "" After displaying ' the error message at Session("CustomError") Session("CustomError") = "" %> </STRONG> </FONT> </BODY> </HTML>

Happy Programming!

Related Articles
  • Dynamic Client Side JavaScript Validation
  • Server Side Validation Code
  • WebWeekly: Validating Data in your Forms
  • Form Validation Using Javascript
  •  


    This article was written by Abd Shomad, currently a student of School of Telecommunication Technology (STTTelkom) Bandung, Indonesia. Abd is involved in many research projects on Internet related science and often leads some real work teams during his studies.


    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