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.

Graphic Designer / Web Designer / Graphic Artist
Next Step Systems
US-IL-Des Plaines

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

    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

    Whitepapers and eBooks

    Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
    IBM Solutions Brief: Go Green With IBM System xTM And Intel
    HP eBook: Simplifying SQL Server Management
    IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
    Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
    Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
    Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
    Intel PDF: Quad-Core Impacts More Than the Data Center
    Intel PDF: Virtualization Delivers Data Center Efficiency
    Go Parallel Article: PDC 2008 in Review
    Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
    Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
    Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
      PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
    Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
    Go Parallel Article: Q&A with a TBB Junkie
    IBM Whitepaper: Innovative Collaboration to Advance Your Business
    Internet.com eBook: Real Life Rails
    IBM eBook: The Pros and Cons of Outsourcing
    Internet.com eBook: Best Practices for Developing a Web Site
    IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
    Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
    IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
    Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
    HP eBook: Guide to Storage Networking
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES