Understanding The JavaScript Event Model (part 1) - Flavour Of The Month (Page 7 of 9 )
You can also use the onFocus and onBlur handlers to perform actions when the user tabs into or out of a form field. Here's a quick example:
<html>
<head>
</head>
<body>
<form action="submit.php" method="post">
<input type="text" name="box" value="Click me" size="20"
onFocus="document.forms[0].box.value='Now click outside'"
onBlur="document.forms[0].box.value='Click me'"> </form>
</body>
</html>
In this case, every time the user clicks inside or outside the text field, either the onFocus() or onBlur() handler is triggered and an appropriate message displayed.
The next example reworks the one on the previous page to perform field validation when the user tabs out of each field on a form:
<html>
<head>
<script language="JavaScript">
// check name field
function checkName()
{
// if empty, pop up alert
if (document.forms[0].name.value == "")
{
alert("Please enter a valid name");
return false;
}
else
{
return true;
}
}
// check email field
function checkEmail()
{
var flag;
var str = document.forms[0].email.value;
// regex to match email addresses
var pattern =
/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/;
var flag = pattern.test(str);
if(!flag)
{
alert ("Please enter a valid email address");
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form action="submit.php" method="post">
Please enter your name.
<br>
<input type="text" name="name" onBlur="checkName()">
<p>
Please enter your email address.
<br>
<input type="text" name="email" size="25" onBlur="checkEmail()"> <br>
<input type="submit" value="Hit me!"> </form>
</body>
</html>
Another very useful event handler, especially in the context of forms, is one you've already seen before - the onClick handler. Consider the following example, which demonstrates it in the context of a radio button,
<html>
<head>
<script language="JavaScript">
function displayFlavour()
{
for(i=0;i<document.displayForm.flavourSelect.length;i++)
{
if(document.displayForm.flavourSelect[i].checked)
{
alert("You picked " +
document.displayForm.flavourSelect[i].value);
}
}
}
</script>
</head>
<body>
<form name="displayForm" action="submit.php" method="post"> Pick a
flavour: <br> <input type="radio" name="flavourSelect"
value="Strawberry" onClick="displayFlavour()">Strawberry
<br>
<input type="radio" name="flavourSelect" value="Raspberry"
onClick="displayFlavour()">Raspberry
<br>
<input type="radio" name="flavourSelect" value="Chocolate"
onClick="displayFlavour()">Chocolate
</form>
</body>
</html>
and this one, which demonstrates it in the context of a check box.
<html>
<head>
<script language="JavaScript">
function displayFlavour()
{
for(i=0;i<document.displayForm.flavourSelect.length;i++)
{
if(document.displayForm.flavourSelect[i].checked)
{
alert("You picked " +
document.displayForm.flavourSelect[i].value);
}
}
}
</script>
</head>
<body>
<form name="displayForm" action="submit.php" method="post"> Pick a
flavour: <br> <input type="checkbox" name="flavourSelect"
value="Strawberry" onClick="displayFlavour()">Strawberry
<br>
<input type="checkbox" name="flavourSelect" value="Raspberry"
onClick="displayFlavour()">Raspberry
<br>
<input type="checkbox" name="flavourSelect" value="Chocolate"
onClick="displayFlavour()">Chocolate
</form>
</body>
</html>
Next: Linking Up >>
More JavaScript Articles
More By Team Melonfire, (c) Melonfire