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


    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