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

0 votes

Best answer

That works great to stop more than two decimals. I'm assuming the 3 in the script is looking at the number of decimals? While I originally was asking about always 'Showing' a specific format, can you adjust this error script to always look for two decimals?

Yes, that is a regular expression looking for three numbers after a period.

So every response must have exactly two decimal places?

So every response must have exactly two decimal places?

Well, if it's possible to just show the error if there is only 1 decimal or more than two, that would be great. I would think allowing a whole number with no decimals would be okay with my client.

Ah, I see.

if (/\.\d$|\.\d{3}/.test($('#[% QuestionName() %]').val().trim())) { strErrorMessage = 'error...'; }

Ahh, that's beautiful! This way it guarantees that I'm getting a valid monetary response, either a whole number or two decimals for cents. This will definitely do the trick. Thanks as always Zach!

Zach, I discovered a problem with this one. if I enter two decimals ending with a 0 (i.e. 7.50) it triggers the error script. Any way around this?

Oh right, we can't use SSI_GetValue if trailing zeroes need to be supported. I've edited my earlier script.

So I got that working on the single numeric question. However, I have a grid question with a constructed row list that needed the same logic. I had it working with the previous script, but can't seem to get this new modification to work with what I had. Here is what I'm trying:

for (var i = 1; i <= 19; i++) { if (/\.\d$|\.\d{3}/.test($('#[% QuestionName() %]_r' + i + '_c1').val().trim())) { strErrorMessage = 'Your response should be in this format $00.00'; } }

We could change up the regex test line, but my usual is to read the constructed list items and write the loop like this:

var rows = $('input[name="hid_row_list_[% QuestionName() %]"]').val().split(','); rows.forEach(function(row){ if (/\.\d$|\.\d{3}/.test($('#[% QuestionName() %]_r' + row + '_c1').val().trim())) { strErrorMessage = 'Your response should be in this format $00.00'; } });

...