Evertjan. said the following on 10/29/2005 5:23 AM:
wrote on 29 okt 2005 in comp.lang.javascript:
<input value=3 readonly><input onblur="
if((z=1*this.value+1*this.previousSibling.value)>5)
alert('Total is '+z+'\nThis is too much!')
">
both inputs have to be on one line for this.previousSibling to work.
getElementById() is preferable.
Why is getElementById() preferable over the forms collection for
accessing form elements?
And onchange would be a better event handler.
function checkTotal(formRef){
total = +formRef.input1.value + +formRef.input2.value
if (total>5){
alert('Total of ' + total + ' is too high.\nPlease try again');
formRef.input1.focus();
}
}
<form action="somePage.php">
<input name="input1" type="text" onchange="checkTotal(this.form)"
value="0">
<input name="input2" type="text" onchange="checkTotal(this.form)"
value="0">
<input name="totalValue" value="0">
But I do agree with your later post that onSubmit is a better place to
validate it.