Java Reference
In-Depth Information
In addition to the common event handlers, such as onfocus and onblur, the Text object has the
onchange, onselect, onkeydown, onkeypress, and onkeyup event handlers.
The onselect event fi res when the user selects some text in the text box.
More useful is the onchange event, which fi res when the element loses focus if (and only if) the value
inside the text box is different from the value it had when it got the focus. This enables you to do things
like validity checks that occur only if something has changed.
You can use the readonly attribute of the <input/> element or the readOnly property of the Text
object to prevent the contents from being changed.
<input type=”text” name=”txtReadonly” value=”Look but don't change”
onfocus=”document.form1.txtReadonly.blur()”
readonly=”readonly”>
The onkeypress, onkeydown, and onkeyup events fi re, as their names suggest, when the user presses a
key, when the user presses a key down, and when a key that is pressed down is let back up, respectively.
Try It Out A Simple Form with Validation
Let's put all the information on text boxes and buttons together into an example. In this example, you
have a simple form consisting of two text boxes and a button. The top text box is for the users' name,
and the second is for their age. You do various validity checks. You check the validity of the age text
box when it loses focus. However, the name and age text boxes are only checked to see if they are empty
when the button is clicked.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 7: Example 4</title>
<script type=”text/javascript”>
function btnCheckForm_onclick()
{
var myForm = document.form1;
if (myForm.txtAge.value == “” || myForm.txtName.value == “”)
{
alert(“Please complete all the form”);
if (myForm.txtName.value == “”)
{
myForm.txtName.focus();
}
else
{
myForm.txtAge.focus();
}
}
else
{
alert(“Thanks for completing the form “ + myForm.txtName.value);
}
Search WWH ::




Custom Search