Have an idea?

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

Restricting to two decimal places - numerics in a grid

Hi ... the answer for the question Zachary answered previously works beautifully for a straight numeric.  But I have to do the same thing in a 1 column grid where the rows depend on a previous question.  This is what I've done (it doesn't work):

var resp1 = $('#[% QuestionName() %]_r1_c1').val().trim();
var resp2 = $('#[% QuestionName() %]_r2_c1').val().trim();
var resp3 = $('#[% QuestionName() %]_r3_c1').val().trim();
var resp4 = $('#[% QuestionName() %]_r4_c1').val().trim();
var resp5 = $('#[% QuestionName() %]_r5_c1').val().trim();
var resp6 = $('#[% QuestionName() %]_r6_c1').val().trim();
var resp7 = $('#[% QuestionName() %]_r7_c1').val().trim();
var resp8 = $('#[% QuestionName() %]_r8_c1').val().trim();
var resp9 = $('#[% QuestionName() %]_r9_c1').val().trim();
var resp10 = $('#[% QuestionName() %]_r10_c1').val().trim();
var resp11 = $('#[% QuestionName() %]_r11_c1').val().trim();
var resp12 = $('#[% QuestionName() %]_r12_c1').val().trim();

if(SSI_GetValue("B1A_1") == 1)
{
    if (resp1 && !/^[0-9]*(\.[0-9]{2})?$/.test(resp)) {
    strErrorMessage = 'Please enter a price for the "Birthday of your children" row';
    }
}
else if(SSI_GetValue("B1A_2") == 1)
{
    if (resp2 && !/^[0-9]*(\.[0-9]{2})?$/.test(resp)) {
    strErrorMessage = 'Please enter a price for the "Birthday of other children" row';
    }
}
else if(SSI_GetValue("B1A_3") == 1)
{
    if (resp3 && !/^[0-9]*(\.[0-9]{2})?$/.test(resp)) {
    strErrorMessage = 'Please enter a price for the "Birthday of family member (not own child)/close friend" row';
    }
}
... etc


A. What am I doing wrong?
B. There's gotta be a better way to code this anyway, right?

I'm on 9.10.1.
related to an answer for: Restricting to two decimal places
asked Dec 1, 2022 by Marion Silver (5,400 points)
Duh!  Just spotted one obvious thing.  Updated to this.

var resp1 = $('#[% QuestionName() %]_r1_c1').val().trim();
var resp2 = $('#[% QuestionName() %]_r2_c1').val().trim();
var resp3 = $('#[% QuestionName() %]_r3_c1').val().trim();
var resp4 = $('#[% QuestionName() %]_r4_c1').val().trim();
var resp5 = $('#[% QuestionName() %]_r5_c1').val().trim();
var resp6 = $('#[% QuestionName() %]_r6_c1').val().trim();
var resp7 = $('#[% QuestionName() %]_r7_c1').val().trim();
var resp8 = $('#[% QuestionName() %]_r8_c1').val().trim();
var resp9 = $('#[% QuestionName() %]_r9_c1').val().trim();
var resp10 = $('#[% QuestionName() %]_r10_c1').val().trim();
var resp11 = $('#[% QuestionName() %]_r11_c1').val().trim();
var resp12 = $('#[% QuestionName() %]_r12_c1').val().trim();

if(SSI_GetValue("B1A_1") == 1)
{
    if (resp1 && !/^[0-9]*(\.[0-9]{2})?$/.test(resp1)) {
    strErrorMessage = 'Please enter a price for the "Birthday of your children" row';
    }
}
if(SSI_GetValue("B1A_2") == 1)
{
    if (resp2 && !/^[0-9]*(\.[0-9]{2})?$/.test(resp2)) {
    strErrorMessage = 'Please enter a price for the "Birthday of other children" row';
    }
}
if(SSI_GetValue("B1A_3") == 1)
{
    if (resp3 && !/^[0-9]*(\.[0-9]{2})?$/.test(resp3)) {
    strErrorMessage = 'Please enter a price for the "Birthday of family member (not own child)/close friend" row';
    }
}
... etc


Still not working though.  When looking at developer tools, I see this:
TypeError: Cannot read properties of undefined (reading 'trim')
When writing code for questions that use constructed lists, it's important to consider what the code is going to do relating to items that did not appear in the constructed list.  If the first row does not appear, the browser basically interprets the code like this:

$('#[% QuestionName() %]_r1_c1') // valid, but produces an empty jQuery object because this element isn't found on the page
$('#[% QuestionName() %]_r1_c1').val() // valid, but returns null because there were no elements on the page to get the value of
$('#[% QuestionName() %]_r1_c1').val().trim() // invalid, trying to call a method on null


There are a few ways we might address this.  The absolute simplest may be to add something to the code to make it so that when "val()" returns null, we replace it with a non-null value.  You could try updating these lines like this and see if that meets your needs:

var resp1 = ($('#[% QuestionName() %]_r1_c1').val() || '').trim();

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.
...