
Lesson 6-Validating forms
One of the most practical uses of JavaScript is validating forms. Since we've just learned how to access forms, in this section, we will talk about:
Introduction
to validating forms
Lets first define what is meant by validating forms. Well, using JavaScript, you can check what a user enters into a form, intercept a form from being submitted and ask the user to retype or fill in the unfilled entries of a form before re-submission. Before, you would have to do this using CGI, which is both something I don't want to touch, and something you don't want to either...you with me? Anyone?
Using
the onBlur event handler
We will now introduce two more event handlers, ones especially for forms, that are used commonly when doing form validations.
| onblur: | Executes JavaScript whenever a user moves with the mouse the focus away from an element within a form. In other words, whenever a person first clicks an element, and then clicks anywhere outside of it. | |
| onsubmit: | Executes JavaScript whenever someone clicks the "submit" button. |
Ok, you need clarification. Onblur is used when you want to check each element (ie: text, textarea boxes ) individually, and ask the user to correct the input before moving on to the next box. The other event, onsubmit, validates the form at the end, as the user presses the submit button. I prefer the later, but its up to you.
Ok, lets see exactly how onblur works. We're going to write a script that checks the first text box , and asks for correction as soon as text has been entered and the user moves the focus onto another box (assuming the person has entered "wrong" data.) Here we go:
| Important: The onBlur event behaves erratically in some browsers when used in a frames page. If you are viewing this lesson in frames mode, the below example may not function smoothly. |
Enter your email address: (Try entering
something without the "@", and you'll get a alert telling you to re-enter the
data as you proceed to the Feedback box.)
<script>
function emailcheck()
{
var string1=document.example.email.value
if (string1.indexOf("@")==-1)
.{
.alert("Please
input a valid email address!")
.document.example.email.focus()
.}
}
</script>
<form
name="example"><input type="text" size="20"
name="email" onblur="emailcheck()">
<strong>Feedback please:</strong>
<textarea name="S1" rows="2"
cols="20"></textarea>
<input type="submit" name="B1" value="Submit">
</form>
Ok, lets have a look at what's inside the <script> tags.
If you've been playing around with the onblur event handler, even with simply the above example, you might have noticed that it can be very annoying at times; the better way is to simply defer the checking until a user presses the submit button, and check for errors all in one scoop. That's where the onsubmit handler comes in.
The "onsubmit" handler is inserted inside the <form> tag, and not inside any one element, unlike "onblur." For example: <form name="george" onsubmit="what_ever"> Lets do an example:
Enter Your name (*required) If you leave the required sections blank, as you click submit, you will be forced to come back and fill this box in.
<script>
<!--
function validate()
{
if ((document.example2.naming.value=="")||
(document.example2.feed.value==""))
.{
.alert
("You must fill in all of the required .fields!")
.return false
.}
}
//-->
</script>
<form name="example2" onsubmit="return validate()">
<input type="text" size="20" name="naming">
<strong>Feedback please: (*required)</strong>
<textarea name="feed" rows="3" cols="25"></textarea>
<strong>Your home address (*NOT required)</strong>
<input type="text" size="35" name="address">
<input type="submit" name="B1" value="Submit">
</form>
Some every important things here:
Ok, in this section, we ran into the string object, so lets list all its properties and methods for quick glance:
| properties | Methods | |||||||||||||||||||
|
|
What's next? Time is precious, so lets look at that...