greatfalls
Technical User
I have many pages of validation (to much to post)of the below piece of a form.
One is the below function, when I would put values that didn't pass the validation into the below script, the alert works but in Netscape, I would get this highlighted and freezing effect. I found that by taking out the window.alerts this didn't happen. (this didn't happen with other window.alert functions in the form) I don't know why this happens, if anyone could figure this out I would be very grateful. (so for now I have blocked out the window.alerts in this function) My second problem is to do a double validation, since after the focus and select it lets you continue, with bad data (non numbers) put into the fields.
I am dealing with a array and am not sure how to do an on submit validaton - with an array. - The fields can be blank, if not blank have to be numbers and of the 5 boxes (fields) - they all don't have to be filled in but can't skip from box 1 to box 3 - so how would I move the below onchange to also be a final validation when hit the submit button?
function validateForm(theForm) {
if (!calcCurrentService(theForm, 'final')) return false;
if ( isAverageSalary(theForm) &&
isPercentIncrease(theForm) &&
isValidAge(theForm) &&
isSalaryHistory (theForm) &&
isValidStartService(theForm) ) {
var aveSalary = theForm.aveSalary.value;
} else {
return false;
}
<input type="text" name="aveSalary" tabindex="27" size="8" onChange="return chkCurrency(this);">
function chkCurrency(fld)
{
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;
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);
}
<input type="image" value="calculate" src="graphics/calculate_e.gif" width="90" height="20" border="0" alt="Calculate" name="Calculate">
One is the below function, when I would put values that didn't pass the validation into the below script, the alert works but in Netscape, I would get this highlighted and freezing effect. I found that by taking out the window.alerts this didn't happen. (this didn't happen with other window.alert functions in the form) I don't know why this happens, if anyone could figure this out I would be very grateful. (so for now I have blocked out the window.alerts in this function) My second problem is to do a double validation, since after the focus and select it lets you continue, with bad data (non numbers) put into the fields.
I am dealing with a array and am not sure how to do an on submit validaton - with an array. - The fields can be blank, if not blank have to be numbers and of the 5 boxes (fields) - they all don't have to be filled in but can't skip from box 1 to box 3 - so how would I move the below onchange to also be a final validation when hit the submit button?
function validateForm(theForm) {
if (!calcCurrentService(theForm, 'final')) return false;
if ( isAverageSalary(theForm) &&
isPercentIncrease(theForm) &&
isValidAge(theForm) &&
isSalaryHistory (theForm) &&
isValidStartService(theForm) ) {
var aveSalary = theForm.aveSalary.value;
} else {
return false;
}
<input type="text" name="aveSalary" tabindex="27" size="8" onChange="return chkCurrency(this);">
function chkCurrency(fld)
{
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;
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);
}
<input type="image" value="calculate" src="graphics/calculate_e.gif" width="90" height="20" border="0" alt="Calculate" name="Calculate">