Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Javascript validation for comparing values in numeric text boxes

Hi. I have a grid row question (Q3) with 2 numeric boxes where respondents enter a lowest price and highest price they would pay for a product.
 
 I have a javascript validation which checks that the lowest price is not more than the highest price....
 
 
 if((document.mainform["Q3_r1_c1"].value) > (document.mainform["Q3_r2_c1"].value))
 {
   strErrorMessage = "The Lower Price needs to be less than or equal to the Higher Price";
 }


The validation works only some of the time.... After a few tests and some head scratching I came to the conclusion that the validation is only checking the first number entered into each box... For example if Low price=5 and High price=20 the error message is generated (because it only compares 5 against 2).

Does anyone know why this is happening and how to resolve it?

Many thanks for any help.
Russell
asked Jul 11, 2012 by rapizel Bronze (1,420 points)
retagged Sep 13, 2012 by Walter Williams

3 Answers

+2 votes
Can you use parseInt for this conditional checking. i.e.
if((parseInt(document.mainform["Q3_r1_c1"].value)) > parseInt((document.mainform["Q3_r2_c1"].value)))
 {
   strErrorMessage = "The Lower Price needs to be less than or equal to the Higher Price";
 }

Let me know if it works.
answered Jul 11, 2012 by Rajesh Rana Gold (23,660 points)
Yes that works! Thanks very much!
+2 votes
The parseInt solution works. Thank you!
answered Jul 11, 2012 by rapizel Bronze (1,420 points)
+2 votes
If you are on version 7 or later, you can use the SSI_GetValue() function to make writing custom validation a little easier.  For example:

if(SSI_GetValue("Q3_r1_c1") > SSI_GetValue("Q3_r2_c1"))
{strErrorMessage = "The Lower Price needs to be less than or equal to the Higher Price";}

It's great for someone like me who is far from being proficient at JavaScript, but delves into custom validation from time to time.
answered Jul 12, 2012 by Brian McEwan Gold Sawtooth Software, Inc. (39,170 points)
...