Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculating form field values

Status
Not open for further replies.

riffy

Programmer
Mar 29, 2001
106
US
i have a form with six text boxes in which a number from 1 to 5 is entered based on the question next to it..
the 7th box, calculates the total of the other 6...i'm thinking i have to use the onchange handler but i'm not sure how to proceed..can you please provide some guidance?
 
<script language=javascript>
function sumIt(){
var sum;
sum = document.formname.elementname1.value + document.formname.elementname2.value + document.formname.elementname3.value + document.formname.elementname4.value + document.formname.elementname5.value + document.formname.elementname5.value + document.formname.elementname6.value;
document.formname.sumelement.value = sum;
}
</script>

call this function onBlur for any and all of the form elements you want it to work on -- (ie. elementname1, 2, 3, etc..)

hope it helps! :)
Paul Prewett
 
thanks but this only concatenates the values together...it didn't add them up...do i need the valueOf() method?
 
Whoops -- you're right, but I don't know the javascript method to convert a string to a number -- :-(

can anyone else help out on this issue?
 
never mind...i figured out how to do it...the lines have to read
(document.formname.elementname.value - 0.0), otherwise it doesn't treat the values as numbers to add and subtract..
thanks anyway...
now my only question is that whether there is an easy way of prompting users to enter only numbers between 1 and 5...i.e., if they enter 6 or higher, it should be turned into a 5 and likewise for numbers less than 1...

thanks
 
here is something I found on the netscape site that might help for issues like the previous in the future


Syntax
parseInt(string,radix)
Parameters
string
A string that represents the value you want to parse.

radix
(Optional) An integer that represents the radix of the return value.

so you'd say:
newValue = parseInt(string, 10) -- will return a base10 type number (rather than hex or octal, which would be 16 and 8, repectively)

and yes, you could just use the existing function, but read all the values into variables first, and then if any of them were >5 or <1, you could just revert them to whatever value you want, plug the number into the text box(es) and then run the function on the new values --

paul




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top