| Forms are often used on web pages to collect information from the
user. VBScript is then used to validate data entry and to submit the information
to a server, another web page, or an email address. Indeed, this may be the
number one use of VBScript. You can use Visual Basic
Scripting to do much of the form processing that you'd usually have to do
on a server. You can also do things that just can't be done on the server.
An example of validation would be script that requires the user to
enter a number within a certain range into a textbox - that is the script
demonstrated here. This is called 'client-side
validation' since it is done by the user's computer (the
client) rather than by the server. For more complex examples of data validation,
check the demo on the next page, which also demonstrates the use of the SUBMIT
button.
Note that although the above button has the value 'Submit' it is
actually a 'normal' push button and not a 'Submit' button.
If a Submit button had been used, the example would never
see the data to check it—everything would go immediately to the server. Avoiding
the Submit button lets you check the data, but it doesn't submit the
data to the server. That requires an additional line of code (see further down
this page).
-
<script LANGUAGE="VBScript">
- <!--
- Sub
Button1_OnClick
Dim
TheForm
Set TheForm =
Document.ValidForm
If
IsNumeric(TheForm.Text1.Value) Then
If TheForm.Text1.Value < 1 Or
TheForm.Text1.Value > 10 Then
MsgBox "Please enter a
number between 1 and 10."
Else
MsgBox "Thank
you."
End If
Else
MsgBox "Please enter a numeric
value."
End
If
End
Sub
-->
</script>
|
The Value property of the text box is used to
check the entered value. To get the Value property, the code has to qualify the
reference to the name of the text box.
You can always write out the full reference Document.ValidForm.Text1.
However, where you have multiple references to form controls, you'll want to do
what was done here. First declare a variable. Then use the Set
statement to assign the form to the variable TheForm. A regular assignment
statement, such as Dim,
doesn't work here; you must use Set to preserve the reference to an object.
Using Numeric Values
Notice that the example directly tests the value against a number: it uses
the IsNumeric function to make sure the string in the text box is a number. Although
VBScript automatically converts strings and numbers, it's always a good practice
to test a user-entered value for its data subtype and to use conversion
functions as necessary. When doing addition with text box values, convert
the values explicitly to numbers because the plus sign (+)
operator represents both addition and string concatenation. For example, if
Text1 contains "1" and Text2 contains "2", you see the following results:
A = Text1.Value + Text2.Value ' A is "12"
A = CDbl(Text1.Value) + CDbl(Text2.Value) ' A is 3
Validating and Passing Data Back to the
Server
The simple validation example uses a plain button control. If a Submit
control was used, the example would never see the data to check it—everything
would go immediately to the server. Avoiding the Submit control lets you check
the data, but it doesn't submit the data to the server. That requires an
additional line of code (the following code is NOT yet demonstrated on this
page, but check back soon...) :
- <SCRIPT LANGUAGE="VBScript">
- <!--
Sub
Button1_OnClick
Dim
TheForm
Set TheForm =
Document.ValidForm
If
IsNumeric(TheForm.Text1.Value) Then
If TheForm.Text1.Value < 1 Or
TheForm.Text1.Value > 10 Then
MsgBox "Please enter a
number between 1 and 10."
Else
MsgBox "Thank
you."
TheForm.Submit ' Data
correct; send to server.
End If
Else
MsgBox "Please enter a numeric
value."
End
If
End
Sub
-->
</SCRIPT>
|
To send the data to the server, the code invokes the Submit method on
the form object when the data is correct. From there, the server handles the
data just as it otherwise would—except that the data is correct before it gets
there. Find complete information about the Submit method and other methods in
the Internet Explorer Scripting Object Model documentation, which can be found
on the Microsoft® Web site (http://www.microsoft.com/). |