Have an idea?

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

Currency question

Sounds like a simple question but in my experience I can't recall having to use it ever with Sawtooth Software.

If I needed to capture a dollar and cents amount in a numeric question, what is the best method? I would like it to look like a currency question if possible.

I need a range from $0.01 to $999.99.

Examples of data entry I would desire ...

$10 as $10.00
5 cents as $0.05
$123.47 as $123.47
$6.50 as $6.50
$22.22 as $22.22

and so on ...

Any suggestions would be much appreciated.
asked Apr 11, 2016 by Paul Moon Platinum (59,930 points)
The easiest option would be to use a numeric question, allow decimals, and finally use custom verification to make sure only two decimal places are allowed.  This will allow the user to type in inputs like "10", "123.47", "6.50", and "22.22".  You could even place a dollar sign to the left of the numeric field to indicate to users to not use their own dollar sign.

If you want to be able to accept answers like "2 dollars and 5 cents", then you'll have to use an open end question with some regex.  I can try to create such a regex today.
Thanks Zachary. If you have a handy piece of script using regex, that would be wonderful. I want it to force the respondent to enter the 2 decimal digits.

Appreciate your assistance.

1 Answer

0 votes
I think I've got it.

To start, create an open end question and a free format question on the same page.  Add a hidden decimal variable to the free format; then, give the free format this code (replacing 'FFQuestion_Var' with the variable's name):

<input name="FFQuestion_Var" id="FFQuestion_Var" type="hidden" value="">


In the open end, use this custom JavaScript verification (replacing 'FFQuestion_Var' and 'OEQuestion'):

$(document).ready(function(){
    var response = SSI_GetValue('OEQuestion');
    
    // Look for the dollar
    var dollar = null;
    
    var dollarRegex = /(\$)|(dollar)/i;
    var dollarNumericRegex = /\$([0-9]{0,3})($|[^0-9])/;
    var dollarTextRegex = /(^|[^0-9])([0-9]{1,3}) +dollars?/i;
    
    if (dollarNumericRegex.test(response))
    {
        dollar = response.match(dollarNumericRegex)[1];
        if (dollar == '')
        {
            dollar = 0;
        }
    }
    else if (dollarTextRegex.test(response))
    {
        dollar = response.match(dollarTextRegex)[2];
    }
    
    if (dollar == null && dollarRegex.test(response))
    {
        strErrorMessage = 'Invalid dollar amount.';
        return;
    }
    
    // Look for the cent
    var cent = null;
    
    var centRegex = /(\.)|(cent)/i;
    var centNumericRegex = /\.([0-9]{2})($|[^0-9])/;
    var centTextRegex = /(^|[^0-9])([0-9]{1,2}) +cents?/i;
    
    if (centNumericRegex.test(response))
    {
        cent = response.match(centNumericRegex)[1];
    }
    else if (centTextRegex.test(response))
    {
        cent = response.match(centTextRegex)[2];
    }
    
    if (cent == null && centRegex.test(response))
    {
        strErrorMessage = 'Invalid cent amount.';
        return;
    }
    
    // Must have dollar and/or cent
    if (dollar == null && cent == null)
    {
        strErrorMessage = 'Could not detect currency value.';
        return;
    }
    
    // Compile final value
    if (dollar == null)
    {
        dollar = 0;
    }
    
    if (cent == null)
    {
        cent = 0;
    }
    
    var final = parseInt(dollar) + parseInt(cent)/100;
    
    // Final value must be (0.00, 999.99)
    if (final == 0)
    {
        strErrorMessage = 'Value cannot be $0.00.';
        return;
    }
    
    else if (final >= 1000)
    {
        strErrorMessage = 'Value cannot be greater than $999.99.';
        return;
    }
    
    // Set free format
    $('#FFQuestion_Var').val(final);
})


Here are a few examples of acceptable inputs:

$123.45
$123
$0.45
$.45
.45
$123 and .45
$123 and 45 cents
123 dollars and 45 cents
123 DOLLARS AND 45 CENTS
1 dollar
1 cent


You should test this code thoroughly to make sure it works as you want.  For example, it can handle "dollar" and "dollars," but not "dolar" or "doller."  If you have more specific details about what should or should not be permitted, I can update the regex.
answered Apr 11, 2016 by Zachary Platinum Sawtooth Software, Inc. (75,550 points)
Thanks very much Zachary.  I will give this a try although I am wanting to accept numeric responses only. I would not expect the respondent to enter any text at all other than the decimal point. I would like to insert the "$" symbol before the data entry box. Happy to hear your thoughts.
Ah, I see.  I got the wrong impression from your second data entry example.

To accomplish what you're going for, you can just use a numeric question.  In the question's settings, enable "Allow Decimal Answers" and set the min and max to 0.01 and 999.99, respectively.  You will then need this custom JavaScript verification:

var response = String(SSI_GetValue('NumericQ'));
var regex = /\.[0-9][0-9]$/;
if (!regex.test(response))
{
    strErrorMessage = 'Error text here.';
}


Finally, all you need to add the dollar symbol before the field is to put "$" in Question Text > Question.
Thanks Zachary. Almost got it working 100%. Your regular expression and this one I was fiddling with
 /^\d+\.\d{0,2}$/;

have the same problem.

They both handle the data entry I require except for these combination types ...

$100.00
$6.50

The double zero for the cents or the last digit for the cents appear to generate the error when it shouldn't appear. Is there something we can do to the regular expression to allow these combinations to be entered successfully.

Thanks once again. Your assistance is very welcomed.
SSI_GetValue keeps simplifying these answers (e.g., 6.50 = 6.5).  So instead of using that function, we should just rip the value from the HTML directly.

$(document).ready(function(){
    var response = $('#NumericQ').val();
    var regex = /\.[0-9][0-9]$/;
    
    if (!regex.test(response))
    {
        strErrorMessage = 'Error text here.';
    }
})
Sorry Zachary to bother you but now all valid entries like $1.23 and $999.99 fail? Any suggestions?
I'm not seeing what you're seeing.  When I set the custom JS verification to the code I put an hour ago, both 1.23 and 999.99 are accepted.  Can you provide further detail?
Thanks Zachary for correcting my error. Left off the hash. Much appreciated.
...