When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Wednesday, July 28, 1999

Using the Dictionary Object for Data Collection & Validation

By Julian

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.

- continued -

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. For more information about client-side validation using JavaScript, be sure to check out Form Validation Using Javascript.

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 call 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 Else objErrors.Add name, value End If End Function Function HandleArray(Elements) If objDict.Count >= Elements Then HandleArray = True Else HandleArray = False End If End Function ' The Iterate Sub iterates through each key in a dictionary, writing out each one as it goes. Sub Iterate(Dictionary) Dim k, i k = Dictionary.Keys For i = 0 To Dictionary.Count - 1 Response.Write k(i) & "<br>" Next End Sub '*******************************************' ' ******* 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 ' Success! ' 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: Response.Write "<h2>Thanks for filling out our survey.</h2>" objDict.RemoveAll objErrors.RemoveAll Set objDict = Nothing Set objErrors = Nothing Else If objDict.Count > 0 Then Response.Write "<h2>So far you have correctly filled out:</h2>" Iterate(objDict) End If Response.Write "<h3>Please fill out the following:</h3>" Iterate(objErrors) End If %>

Well, that's all there is to it. Be sure to download the code and play around with it!

Happy Programming!

Validation Articles
For more information on email address and form validation, check out these great articles!

  • The Forgotten Art of Email Address Validation
  • ASP/JScript Email Validation
  • A Server-Side Email Validation Routine
  • Another Email Validation Routine
  • Form Validation Using JavaScript
  • Using the Dictionary Object Data Collection and Validation

  • 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.

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article