PDA

View Full Version : validating sum of form objects values



Keith Stanley
Mar 27th, 2003, 11: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
Mar 27th, 2003, 11: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
Mar 27th, 2003, 11:50 PM
try this:


If (eval(form.name1.value)+eval(form.name2.value)==100)

HairyTeeth
Mar 28th, 2003, 12:26 AM
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
Mar 28th, 2003, 12:32 AM
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
Mar 28th, 2003, 12:43 AM
All my dental advice comes from my hairdresser :thumbsup:

Keith Stanley
Mar 31st, 2003, 02:21 AM
The suggestion by HairyTeeth worked. Many thanks.javascript:smilie(':)')