Document Collections

The Document object allows access to many page elements besides the background color. Accessing these elements is typically done through a collection, a special kind of object in the Internet Explorer object model. A collection represents a group of objects that are of the same class. It allows easy access to every member of the class. For example, using the Forms collection, you can access and perform functions on any form or every form in the document. These functions can include field validation and form submittal. Accessing controls is done through the form that contains them. In general, any property or method of a control contained on a form can be accessed through the Document and Form objects as follows:

Document.FormName.ControlName.Property[Method]

To practice using forms and controls in VBScript, let's create a simple form with client-side validation. In this example, assume you have a form named frmValidate with two text boxes in an HTML table. One of the text boxes, txtDate, is used to input a date.

Because there are so many different ways of entering a date, you want to validate the date before submission. In this case, use a simple HTML button to call the validation routine in the OnClick event handler. In the OnClick event handler, use the VBScript IsDate function. The IsDate function returns True if the entry in the text box is a valid date. Listing 3-4 provides complete code.

Listing 3-4. Field validation in a form.


<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Developer Studio"> <META HTTP-EQUIV="Content-Type" content="text/html;     charset=iso-8859-1"> <TITLE>Data Validation</TITLE> <SCRIPT LANGUAGE="VBScript"> <!--     Sub cmdValidate_OnClick()              ` Validate the date         If Not (IsDate(Document.frmValidate.txtDate.Value)) Then             MsgBox "Not a valid date!"         End If     End Sub --> </SCRIPT> </HEAD> <BODY> <FORM NAME="frmValidate"> <TABLE>     <TR>         <TH>         Name         </TH>         <TD>         <INPUT NAME="txtName">         </TD>     </TR>     <TR>         <TH>         Date         </TH>         <TD>         <INPUT NAME="txtDate">         </TD>     </TR>     <TR>         <TD COLSPAN=2>         <INPUT TYPE="BUTTON" NAME="cmdValidate" VALUE="Go!">         </TD>         <TD>         </TD>     </TR> </TABLE> </FORM> </BODY> </HTML> 

IE 4.0 supports collections for many other objects, including Links, Anchors, Images, Filters, Forms, Applets, Embeds, PlugIns, Frames, Scripts, and StyleSheets. These collections allow you to access every aspect of an HTML page and the core of Dynamic HTML.



Programming Active Server Pages
Programming Active Server Pages (Microsoft Programming Series)
ISBN: 1572317000
EAN: 2147483647
Year: 1996
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net