Have an idea?

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

JavaScript verification- Numeric input and check box question

Dear All,

I have a grid question in a free format where we would like respondents to input a numeric input  and for their convenience we would like also to enable a single check box  in the same row - to tick if no input to numeric input is required.

I would like to avoid a situation when respondents don't answer to any of the question - not numeric nor check box.  For the verification of the answer I probably need a javascript code , here is my try but it doesn't work for some reason. Can somebody help ?
Thanks a lot !
Robson

 var GOLD1=SSI_GetValue('Q19ff_GOLD1');  // this is numeric input
 var notchange_gold1=SSI_GetValue('q19_ff_select1'); // this is check box
 var GOLD2=SSI_GetValue('Q19ff_GOLD2');  // this is numeric input
 var notchange_gold2=SSI_GetValue('q19_ff_select2');  // this is check box
 


 if(GOLD1>0 && notchange_gold1==1)
{
    strErrorMessage = "You cannot input number to column 1 and  select "will not change" at the same time";
}

 if(GOLD1==0 && notchange_gold1==0)
{
    strErrorMessage = "Please answer to Row 1 ";
}


 if(GOLD2>0 && notchange_gold2==1)
{
    strErrorMessage = "You cannot input number to column 1 and  select "will not change" at the same time";
}

 if(GOLD2==0 && notchange_gold2==0)
{
    strErrorMessage = "Please answer to Row 2";
}

asked Dec 30, 2013 by robson Bronze (700 points)
retagged Dec 30, 2013 by Walter Williams
I have a hunch that a problem (maybe not the problem) is having nested quotation marks inside the set of quotation marks for the first strErrorMessage.  What I would do is first make sure your syntax is correct.  Perhaps remove 3 of the 4 custom validation sets, and just get one that says something like if(SSI_GetValue("Q19ff_GOLD1")==0) {strErrorMessage = "Gold1 is zero";}

This way you can try to eliminate things like assigning your own variables and any other problems that might be compounding on themselves.  If that above works, then gradually add in complexity.  If you then get the first custom validation to work, then go in and add the others.

1 Answer

+3 votes
I've had to use this type of setup quite a bit.

This is an example of the code I use when I have a numeric response (Q1_NUM) and a check box for refused or don't know (Q1_DK).  I set both questions to not force a response and use this code:

if ((document.mainform["Q1_NUM"].value).length == 0 && SSI_GetValue ("Q1_DK_1") == 0)
{
var strErrorMessage = "You must enter a valid number OR select Don't Know";
}
if ((document.mainform["Q1_NUM"].value).length > 0 && SSI_GetValue ("Q1_DK_1") == 1)
{
var strErrorMessage = "You must enter a valid number OR select Don't Know, NOT BOTH";
}
answered Dec 30, 2013 by Jay Rutherford Gold (31,740 points)
Thank you Brian and Jay. Actually your both approaches did help.
Apart from other minor things the issue was that Variable names in the HTML tag should be exactly the same as they were set up, exp. lower and upper case : q19ff  vs. Q19ff. Then it can be correctly recognized by JavaScript Verification and Jay's code works perfectly. Thanks a lot again !
Following above e-mail , in another project it took me 3 hours to find an error so I would like to share with all who may be interested. When you do your a free format table avoid to use "id" signature , here is  part of the table which works with Java verfication:
<tr>
  <td > No device or surgical treatment just lifestyle changes   </td>
  <td style="text-align:center"> [ ] </td>
  <td style="text-align:center">  <input name="Q22_moderate1"  type="numeric" size="5"> &nbsp;% </td>
  <td style="text-align:center"> <input name="Q22_select1"  type="checkbox" value="1">  </td>
</tr>


and this one due to id="Q22_moderate1" will not work.


 <tr>
  <td > No device or surgical treatment just lifestyle changes   </td>
  <td style="text-align:center"> [ ] </td>
  <td style="text-align:center">  <input name="Q22_moderate1" id="Q22_moderate1" type="numeric" size="5"> &nbsp;% </td>
  <td style="text-align:center"> <input name="Q22_select1" id="Q22_select1" type="checkbox" value="1">  </td>
</tr>

...