【JS】 Set Thousand Separator in Text Field
>
>
【JS】 Set Thousand Separator in Text Field

JS - Set Thousand Separator in Text Field

JS - Set Thousand Separator in Text Field

Adding thousand separators to numbers in text fields can greatly enhance user experience on your website. In this post, we’ll explore how to achieve this using JavaScript. We’ll provide two examples for both text fields and select fields, each with line-by-line explanations to ensure you understand the process thoroughly.

Table of - contents

No.
Title
1
Code Example 1
2
Code Example 2
3
Conclusion

Auto-Set Thousand Separator in Text Field.

Example - 1.

<input type="text" id="numberInput" oninput="autoAddSeparator(this)">

<script>
function autoAddSeparator(input) {
    const value = input.value.replace(/,/g, ''); // Remove existing separators
    input.value = Number(value).toLocaleString();
}
</script>

Explanation.

a) Create an input field with an ‘id’ of “numberInput” and add the ‘oninput’ attribute to trigger the ‘autoAddSeparator’ function.
b) In the JavaScript code, the ‘autoAddSeparator’ function is called whenever the input’s value changes.
c) The function first removes any existing thousand separators by using a regular expression (/,/g) to replace them with an empty string.
d) It then converts the cleaned value to a number and formats it with thousand separators using ‘toLocaleString()’.

Example - 2

<select id="numberSelect" onchange="autoAddSeparatorToSelect(this)">
    <option value="1000">1,000</option>
    <option value="10000">10,000</option>
    <option value="100000">100,000</option>
</select>

<script>
function autoAddSeparatorToSelect(select) {
    const selectedOption = select.options[select.selectedIndex];
    const value = selectedOption.value.replace(/,/g, ''); // Remove existing separators
    selectedOption.text = Number(value).toLocaleString();
}
</script>

Explanation.

a) Create an input field with an ‘id’ of “numberSelect” and add the ‘onchange’ attribute to trigger the ‘autoAddSeparatorToSelect’ function when the selection changes.
b) In the JavaScript code, the ‘autoAddSeparatorToSelect’ function is called when a new option is chosen.
c) It retrieves the selected option and removes any existing thousand separators by using a regular expression (/,/g) to replace them with an empty string.
d) The cleaned value is converted to a number and formatted with thousand separators using ‘toLocaleString()’. The option’s text is updated with the formatted value.

3 - Conclusion.

Automatically setting thousand separators in text and select fields with JavaScript is a valuable user experience enhancement. These examples show you how to format numerical data on the fly, without the need for a separate button to trigger the action. Implementing this feature will make your website more user-friendly and accessible, ensuring a smoother user interaction.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Search

.
Xiao. tian
.

Piano - Music

.

Recent - Post

.
0 0 votes
Article Rating

Start typing and press Enter to search

Shopping Cart
0
Would love your thoughts, please comment.x
()
x