Adding Numbers Dynamically

R

rcopeman

I need to add two number together and check that the value does not
exceed a third value as a form is being filled in.

Can I do this with onfocusout?

If so how?!

Thanks for any help.
 
E

Evertjan.

wrote on 29 okt 2005 in comp.lang.javascript:
I need to add two number together and check that the value does not
exceed a third value as a form is being filled in.

Can I do this with onfocusout?

<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.
 
E

Evertjan.

wrote on 29 okt 2005 in comp.lang.javascript:
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.
Thanks for the quick reply and a perfect solution.

[please always quote on usenet, this is not email]


However, not to confuse the user, it seems better to me(!) to test on
submitting, and block the submit by a "return false" if the values are not
correct.

<form onsubmit="return testSumOfInputs()">
 
R

Randy Webb

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.
 
E

Evertjan.

Randy Webb wrote on 29 okt 2005 in comp.lang.javascript:
Why is getElementById() preferable over the forms collection for
accessing form elements?

Not over the forms collection,
but over the siblings without the collection

<input><input> are eachothers siblings in IE

<input>
<input> are not
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top