greatfalls
Technical User
I have included a script and a form. The problem with it is that when it is inside the whole form it "highlights" the whole form - I guess the onfocus does this - it only happens in Netscape. It cannot skip any boxes, which the script catches, but then it lets you go to the output without stopping you. Same problem if I enter a none number - it alerts you but lets you continue. Any suggestions - Thanks
<html><head>
<script language="JavaScript">
<!-- // Begin
function chkCurrency(fld) {
// insure currency input (no numeric sign allowed)
var str = fld.value.replace(/\$|\,|\s/g,'');
if (!/^[0-9]+\.?[0-9]{0,2}$/.test(str)) {
window.alert("This must be a valid currency value."
fld.focus();
fld.select();
return false;
}
fld.value = fmtCurrency(str);
var fldAry = new Array();
fldAry[0] = fld.form.salHistoryCur;
fldAry[1] = fld.form.salHistoryLessOne;
fldAry[2] = fld.form.salHistoryLessTwo;
fldAry[3] = fld.form.salHistoryLessThree;
fldAry[4] = fld.form.salHistoryLessFour;
// check field priorities
if (fld.name=="aveSalary"
for (var i=0; i<fldAry.length; i++) {
fldAry.value = "";
}
} else {
var cnt = 0, sum = 0;
for (var i=0; i<fldAry.length; i++) {
if (fldAry.value.length == 0) break;
cnt++;
sum += Number(fldAry.value.replace(/\$|\,|\s/g,''));
}
for (i; i<fldAry.length; i++) {
if (fldAry.value.length > 0) break;
}
if (i<fldAry.length) {
window.alert("Cannot skip a salary history year."
fldAry[cnt].focus();
fldAry[cnt].select();
return false;
}
str = (sum/cnt).toString();
fld.form.aveSalary.value = fmtCurrency(str);
}
return true;
}
function fmtCurrency(str) {
// convert numeric string to currency format
cents = Math.floor((str*100+0.5)%100);
str = Math.floor((str*100+0.5)/100).toString();
if (cents < 10) cents = "0" + cents;
// return currency format
return (str + '.' + cents);
}
// End --></script>
</head>
<BODY >
<form name="form" method="post" action="output_e.cfm">
<table cellpadding="0" cellspacing="0" border="0" align="center" width="512">
<tr> <td><br>
<table width="508" border="0" cellspacing="0" cellpadding="0">
<tr><td></td> <td bgcolor="CED6E7">
<table width="508"
cellpadding="5" bgcolor="#CED6E7" cellspacing="0" border="0">
<tr bgcolor="#CED6E7">
<td class="out" height="20" valign="middle" colspan="6">
<table width="498" cellpadding="0" cellspacing="0" border="0"> <tr> <td class="out" colspan="5">Salary History: </td>
</tr> <tr> <tr>
<td width="98" align="center" nowrap>
<input type="text" name="salHistoryCur" tabindex="20" size="6" onChange="return chkCurrency(this);"></td>
<td width="98" align="center">
<input type="text" name="salHistoryLessOne" tabindex="21" size="6" onChange="return chkCurrency(this);"> </td>
<td width="98" align="center"> <input type="text" name="salHistoryLessTwo" tabindex="22" size="6" onChange="return chkCurrency(this);">
</td><td width="98" align="center">
<input type="text" name="salHistoryLessThree" tabindex="23" size="6" onChange="return chkCurrency(this);"> </td>
<td width="98" align="center"> <input type="text" name="salHistoryLessFour" tabindex="24" size="6" onChange="return chkCurrency(this);">
</td></tr><tr> <td class="out" colspan="5" height="25">Average Salary: </td>
</tr> <tr> <td colspan="3"> <span class="label">$</span>
<input type="text" name="aveSalary" tabindex="27" size="8" onChange="return chkCurrency(this);"> </td> </tr>
</table> <div align="right"><br>
<input type="image" tabindex="20" label="calculate" value="calculate" src="graphics/calculate_e.gif" width="90" height="20" border="0" alt="Calculate" name="Calculate"></div></td></tr></table></td><td ></td>
</tr></table> </td> </tr> </table> </form>