To read the article online, visit http://www.4GuysFromRolla.com/webtech/020799-1.shtml

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!


<%
'---------------------------------------------------------------------------
' 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!


Article Information
Article Title: 4GuysFromRolla.com - Server Side Validation Code
Article Author: Abd Shomad
Published Date: Sunday, February 07, 1999
Article URL: http://www.4GuysFromRolla.com/webtech/020799-1.shtml


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers