I would like to add a thousands separator to a numeric field as people type in an answer. Show Table of Contents
In SSI Web this can be done with some custom JavaScript, but there are couple of tricky aspects to implementing it. The first is that numeric questions cannot save a number containing commas—only numbers are allowed. So the question will need to be a text question. Another thing to consider is that inserted commas will get saved to the database, which may make data processing/analysis trickier if a number is needed or expected. To be safe, this example uses a free format question that adds commas to input and saves two answers: one with commas and one without commas. To begin, create a free format question and add two variables: a text variable and a numeric hidden variable. (Note: Numeric fields are limited to 999999999 (nine nines), so if more digits are need then make the hidden variable text instead of numeric) Place the required HTML on the page for the variables to display and save correctly. An easy way to make sure the HTML is correct is to make SSI Web to it. Click the pencil icon next to the HTML box on the Variables/Question HTML tab of the free format question to bring up the text editor. Click the hammer and wrench icon at the top of the page and select the variable you want to insert. Now the JavaScript. Click the Advanced button from the free format question and place the following JavaScript in the HTML <head> tab.
Explanation of code
Note: If only a number with commas needs to be stored in respondent data than this can
be done with just a text question instead of a free format question. If that is the case then
Preview:Description:A really simple JavaScript plugin that lets you format a number with a custom character (default: comma) as a thousands separator. How to use it:1. Import the easy-number-separator.js JavaScript library. <script src="easy-number-separator.js"></script> 2. Attach the easyNumberSeparator to the input field you specify and done. <input type="text" class="number-separator" placeholder="Enter Your Number..." /> easyNumberSeparator({ selector: '.number-separator', }) 3. Customize the thousands separator. Default: comma(,). easyNumberSeparator({ selector: '.number-separator', separator: '.' }) 4. If you want to send data to the server, you can set a result input by <input type="text" id="result_input" name=""> easyNumberSeparator({ selector: '.number-separator', separator: ',', resultInput: '#result_input', }) Changelog:06/22/2022
11/21/2021
10/19/2021
10/08/2021
How do you add commas as thousands separator for numbers in JavaScript?To comma-separate thousands in a big number in JavaScript, use the built-in toLocaleString() method. It localizes the number to follow a country-specific number formatting. To separate thousands with commas, localize the number to the USA. How do you add a thousands separator in numbers?Show or hide the thousands separator. Select the cells that you want to format.. On the Home tab, click the Dialog Box Launcher next to Number.. On the Number tab, in the Category list, click Number.. To display or hide the thousands separator, select or clear the Use 1000 Separator (,) check box.. How do you format a thousands separator?The character used as the thousands separator In the United States, this character is a comma (,). In Germany, it is a period (.). Thus one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space. How do you format numbers in JavaScript?JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency. |