View Full Version : validating sum of form objects values
Keith Stanley
03-27-2003, 10:24 PM
In validating a form I have no problem with expressions testing the value of a single form object eg
If (form.name.value=="100")
however testing the sum of multiple form object values does not work, eg
If (form.name1.value+form.name2.value=="100")
where name1 and name 2 are the names of input text fields
I have tried numerous ways round this without success.
Any help gratefully received.
Jason
03-27-2003, 10:28 PM
have you tried decalring a new variable for each before the if statement, then adding the two together and then try the comparison. And then why are you comparing the values to a string. You would need to make sure that the two values added together are outputed in a string format for the comparison to "100" for that to work.
Jason
chrismiceli
03-27-2003, 10:50 PM
try this:
If (eval(form.name1.value)+eval(form.name2.value)==100)
HairyTeeth
03-27-2003, 11:26 PM
You may know this but...textbox values in a form are treated as strings. For example, 10 + 10 = 1010 (not 20). You need to convert the string values to numbers to perform mathematical proceedures by using either...
(a) parseInt(value) for integers or
(b) parseFloat(value) for floating-point numbers (numbers that contain decimal places). For example:
If (parseInt(form.name1.value) + parseInt(form.name2.value)== 100)
Personally, i would use variables like this:
var valOne = parseInt(form.name1.value)
var valTwo = parseInt(form.name2.value)
var total = (valOne + valTwo)
if(total == 100)
//blah blah
else
//blah blah
Anyway, thats my couple of cents worth. Cheers
beetle
03-27-2003, 11:32 PM
Listen to HairyTeeth. I wouldn't use that many variables, but he is right. He know's what's up.
Although I'd be skeptical of any dental advice ;)
HairyTeeth
03-27-2003, 11:43 PM
All my dental advice comes from my hairdresser :thumbsup:
Keith Stanley
03-31-2003, 01:21 AM
The suggestion by HairyTeeth worked. Many thanks.javascript:smilie(':)')
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.