|
<INPUT ...>onChange = "script command(s)"onChangeonChange<TEXTAREA ...><SELECT ...>onChange
The onChange
<SCRIPT TYPE="text/javascript">
<!--
function checkEmail(email)
{
if(email.length > 0)
{
if (email.indexOf(' ') >= 0)
alert("email addresses cannot have spaces in them");
else if (email.indexOf('@') == -1)
alert("a valid email address must have an @ in it");
}
}
//-->
</SCRIPT>
<FORM ACTION="/cgi-bin/mycgi.pl" METHOD=POST>
name: <INPUT NAME="realname"><BR>
email: <INPUT NAME="email" onChange="checkEmail(this.value)"><BR>
destination: <INPUT NAME="desination">
</FORM>
which gives us this form:
BecauseonChange
The onChangeonChangeonChangeonChange
<SCRIPT TYPE="text/javascript">
<!--
function orderTotal(oform, prefix)
{
// set references to fields
var qty = oform[prefix + "_qty"];
var stHold = oform[prefix + "_stHold"];
var price = oform[prefix + "_price"];
var stVis = oform[prefix + "_stVis"];
// only bother if the field has contents
if (qty == "")return;
// if the with is not a number (NaN)
// or is zero or less
// everything goes blank
if(isNaN(qty.value) || (qty.value <= 0))
{
qty.value = "";
stHold.value = "";
}
// else the field is a valid number, so calculate the
// total order cost and put that value in the
// hidden subtotal field
else
stHold.value = (Math.round(qty.value * price.value * 100))/100;
// call the routine which checks if the
// visible subtotal is correct
visTotal(oform, prefix);
}
// checks if the visible subtotal is correct
// ie, if it equals the hidden subtotal field
function visTotal(oform, prefix)
{
var stHold = oform[prefix + "_stHold"];
var stVis = oform[prefix + "_stVis"];
if (stVis.value != stHold.value)
stVis.value = stHold.value;
}
// -->
</SCRIPT>
<FORM ACTION="/cgi-bin/mycgi.pl">
<TABLE BORDER CELLPADDING=3>
<!-- table titles -->
<TR BGCOLOR="#99CCFF">
<TH>item</TD>
<TH>quantity</TD>
<TH>price</TD>
<TH>total</TD>
</TR>
<!-- v-neck sweater -->
<TR BGCOLOR="#FFFFCC">
<TD>v-neck sweater</TD>
<TD><INPUT
TYPE=TEXT
NAME="vn_qty"
SIZE=3
onChange="orderTotal(this.form, 'vn')"
></TD>
<TD><INPUT TYPE=HIDDEN NAME="vn_price" VALUE="33.95">$33.95</TD>
<TD ALIGN=RIGHT>
<INPUT TYPE=HIDDEN NAME="vn_stHold">
<INPUT
TYPE=TEXT
NAME="vn_stVis"
SIZE=10
onChange="visTotal(this.form, 'vn')"
></TD>
</TR>
<!-- JoAnn style blazer -->
<TR BGCOLOR="#FFFFCC">
<TD>JoAnn style blazer</TD>
<TD><INPUT
TYPE=TEXT
NAME="ja_qty"
SIZE=3
onChange="orderTotal(this.form, 'ja')"
></TD>
<TD><INPUT TYPE=HIDDEN NAME="ja_price" VALUE="99.95">$99.95</TD>
<TD ALIGN=RIGHT>
<INPUT TYPE=HIDDEN NAME="ja_stHold">
<INPUT
TYPE=TEXT
NAME="ja_stVis"
SIZE=10
onChange="visTotal(this.form, 'ja')"
></TD>
</TR>
</TABLE>
<P><INPUT TYPE=SUBMIT VALUE="submit">
</FORM>
which gives us this form:
Copyright 1997-2002 Idocs Inc. Content in this guide is offered freely to the public under the terms of the Open Content License and the Open Publication License. Contents may be redistributed or republished freely under these terms so long as credit to the original creator and contributors is maintained.