<HTML><HEAD>
<!--
------------------------
Elementary Validation #1
------------------------
-->
<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers
/*
THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
Copyright (c)1998 by Charles River Media. All Rights Reserved.
This applet can only be re-used or modifed by license holders of the
JavaScript Cookbook CD-ROM. Credit must be given in the source
code and this copyright notice must be maintained. If you do
not hold a license to the JavaScript Cookbook, you may NOT
duplicate or modify this code for your own use.
Use at your own risk. No warranty is given or implied of the suitability
of this applet for any specific application. Neither Erica Sadun nor
Charles River Media will be held responsible for any unwanted effects
due to the use of this applet or any derivative.
*/
function validate()
{
if (document.forms[0].F1.value.length == 0)
{
alert('You can not submit an empty text field')
return false
}
else
{
alert('About to submit...')
return true
}
}
<!-- done hiding --></SCRIPT></HEAD>
<BODY bgcolor="ffffff" link="0000ff" vlink="770077"
onLoad="document.forms[0].F1.focus();document.forms[0].F1.select()">
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
ALIGN = LEFT>Validation #1: Blank Fields</H1></FONT>
<BLOCKQUOTE>
<FONT COLOR="770000">
This script will not submit unless the text field has data.
</FONT>
<FORM onSubmit='return validate()'>
<INPUT TYPE='TEXT' NAME='F1'>
<INPUT TYPE='SUBMIT'>
</FORM>
</BLOCKQUOTE>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
This form's text field is placed in focus when the page loads
using the <FONT COLOR="770000">focus()</FONT> method. Any text
in the field is selected with the <FONT COLOR="770000">select()</FONT>
method.
The form's <FONT COLOR="770000">onSubmit</FONT> event
calls the function <FONT COLOR="770000">validate()</FONT>
which ensures that the text field has text in it of some
sort.
</FONT>
<FONT COLOR="770000"><PRE>
<BODY onLoad="document.forms[0].F1.focus();document.forms[0].F1.select()">
function validate()
{
if (document.forms[0].F1.value.length == 0)
{
alert('You can not submit an empty text field')
return false
}
else
{
alert('About to submit...')
return true
}
}
</PRE></FONT>
<h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>