Have an idea?

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

Numeric with comma

This solution has been helpful for me, however I am still missing something.  I need to restrict other specify boxes as well as grid rows to only allow numbers and commas (they are various questions that require a currency response). When I try to implement the regex similar to the one that allows optional decimal points (replacing with comma), the question allows both letters and numbers, and will only show the error message if there are no numbers.

How should I adjust the JavaScript to allow respondents to put in only numbers and commas?

var resp = SSI_GetValue('[% QuestionName() %]_3_other');
var regex = /([0-9]+,?[0-9]*)|([0-9]*,?[0-9]+)/;
if (resp && !regex.test(resp)) {
    strErrorMessage = 'Error.';
}
asked Oct 17, 2017 by anonymous

1 Answer

0 votes
This regular expression will allow only numerical characters and commas:

/^[0-9,]*$/


This more complex regex will allow only numeric characters with commas every three characters from the right:

/^[0-9]{1,3}(,[0-9]{3})*$/
answered Oct 17, 2017 by Zachary Platinum Sawtooth Software, Inc. (206,100 points)
...