Using the Dictionary Object for Data Collection & Validation
This is an example of how one could use the Dictionary object to
handle processing of forms or the gathering of data. It is
usually better to do form validation/processing on the client, but for
the sake of this demo we'll imagine we are doing some kind of database
lookup on the server side.
Basically once the Dictionary object "fills up" then specific code is executed.
You can build a form for use with this page or you can run it by itself directly
on your server to get the basic idea.
If you are unfamiliar with Dictionary object see the
WebDaily Dictionary article on this site.
Well, here's the code. It allows you first enter your string form variables into the dictionary object.
You can specify if the strings can be null or not, and how long they must be. For example, you might
want to make a password field be at least 8 characters long. Once you enter all the variables you
HandleArray to see if there were any errors. If there weren't then you can go ahead an update
the database, or do whatever. If there were errors, though, a nice looking error message is displayed.
Dim objDict, objErrors, firstname, lastname, phone, email
Set objDict = CreateObject("Scripting.Dictionary")
Set objErrors = CreateObject("Scripting.Dictionary")
' CheckIn Function:
' checks to see if the strings are valid and if so adds them to the dictionary.
' And if not valid, then add them to the error dictionary.
Function CheckIn(name, value, canbenull, length)
' You can go crazy in here with whatever data checks you'd like.
If (value <> "") And (canbenull = False) And (Len(value) >= length) Then
objDict.Add name, value
objErrors.Add name, value
If objDict.Count >= Elements Then
HandleArray = True
HandleArray = False
' The Iterate Sub iterates through each key in a dictionary, writing out each one as it goes.
Dim k, i
k = Dictionary.Keys
For i = 0 To Dictionary.Count - 1
Response.Write k(i) & "
' ******* Get form values *******
' You can build a form to submit to this page
' These data types are only examples...
firstname = Request.Querystring("fname")
lastname = Request.Querystring("lname")
phone = Request.Querystring("phone")
email = Request.Querystring("email")
' If the name value pairs pass the CheckIn Function then
' they are added to the dictionary object
Call CheckIn("First Name", firstname, False, 6)
Call CheckIn("Last Name", lastname, False, 6)
Call CheckIn("Phone Number", phone, False, 6)
Call CheckIn("Email Address", email, False, 6)
' ******* Check HandleArray *******
' When HandleArray is true then we are successful in our data collection.
If HandleArray(4) = True Then
' You could go to the next page here.
' ADVANCED: You could place the dictionary object in a session variable
' for use in filling out multi-page forms.
' Be forewarned! This can be very hazardous to server memory.
' Or display a message:
Thanks for filling out our survey."
Set objDict = Nothing
Set objErrors = Nothing
If objDict.Count > 0 Then
So far you have correctly filled out:"
Please fill out the following:"
Well, that's all there is to it. Be sure to download the code and play around with it!
Attachments:Download the Code for this Article in Text Format
Julian is a software developer who currently spending his time developing
a web-based project management software using Java. Most of his experience
is in the e-commerce/Internet development arena and prefers to develop
with Java, ASP, JSP, XML, Oracle, SQL Server, and UNIX (in no particular
order). Previous to his current endeavors, Julian worked for an online
learning company where he designed and implemented a large, distributed
online learning application.