Client-Side Form Field Validation with VBScript
Form field validation is one of the most
important functions in making successful web-based applications. It protects
your database from errors, filters bad data, and forces the user to think twice
before submitting data with typos. For some reason though, documentation in many
think it might be that field validation scripting is somewhere between learning
the basics but not complicated enough for the advanced literature. The ASP books
cover server-side validation (Remember? It's a server-side
scripting language.) but this is not always an ideal approach to validating
data. The problems with server-side validation are that it bogs down your server's
resources when the user could handle the routines locally, and it offers a
slower response time for the user when it actually finds errors. This tutorial
is designed to take the load off the servers for a while and get the beginning
weblication programmer closer to the intermediate level and beyond. Have fun!
The goal of the script:
When I click the submit button I want my script
to check the field data in my form and either:
- Submit the data if it all looks good
- Show an alert box and do not send the data.
You need a few tools:
- The OnSubmit event.
- A form name in your HTML code (e.g. <form
- A Submit Button: (e.g. <input
- An Input Box with name: (e.g. <input
- Some "If..Then" statements
- An "Alert Box"
- The secret. (I love suspense.)
Let's see it work before we get into the guts of
- Type your name and submit!
- Enter nothing and submit!
- Enter one character and submit!
<-- Option Explicit
header = aspalliance.com
first thing you want to do is tell the browser that the code you are
about to run is VBScript. The next thing you want to do is make
some variables that we are going to use in our routine.
The "validation" variable is going to
be used to help us determine whether we should send the form or not.
It will can be equal to either "True" or "False" (By
the way, that's pretty much the secret ingredient!)
The "Header" variable I will use to
hold the string information for a MsgBox property.
next thing you want to do is make a function that is
going to be called when a user clicks the "Submit Info"
button. The function needs to be bound to your form name and the OnSubmit
event so VB knows what it is that you are trying to submit. I have
named my form...."MyForm"
I have also made my "validation" variable equal to
"True." I will follow this with some
"If...Then" statements that will try to change the variable to
Len(Document.MyForm.MyBox.Value) > 2 Then
MsgBox "You have
entered too many characters! You need fewer characters before I
will submit this form",8, Header
validation = False
first "If...Then" basically says that If the Length
of the value of the contents of Mybox
in the form Myform within the current document
is less than five characters Then show the Message Box(MsgBox).
The properties of the message box include the message string, then the
type of message box(8), and I add the header
message. The next thing I do is make our validation variable equal
to False. You'll see why in a second.
The last thing I do is End this If statement.
(Document.MyForm.MyBox.Value) = "" Then
MsgBox "You have
forgotten to fill in the input box! Why would you want to submit
nothing in a one field form? C'mon, give me something to work
validation = False
this "If..Then" I make sure the user has at least entered
something into the text input box. If they haven't they will get a
message box and once again the validation variable will be made equal to
validation = True Then
last part of this routine is the secret.
All I do is test to see if any "If..Then" statement has
made my validation variable equal to something other than
"True". If the validation
variable equals "True" then MyForm_OnSubmit
also equals "True." If the variable
equals something else other than "True" Then MyForm_OnSubmit
will equal "False." If the Function
equals "False" then the OnSubmit event will not execute and
the user is prompted to fix their mistake. Otherwise, the OnSubmit event
fires and the form data is posted to the page that will
process the submitted form. Simple!
Finally, End your If,
tell VBScript to end the Function, and close the script
with the SCRIPT tag.
Now granted, this is is a very simple example
and in a real-world application you might have ten to twenty form fields to
validate. Although there is potential for your script becoming quite
complicated you can use some subroutines that do some work for you.
Check(Document.MyForm.Company.Value, "Please enter
a company name.")
"Please enter a name.")
Sub Check(ByVal FieldValue, ByVal
If FieldValue = "" Then
message, 8, Header