UPDATED: Code improved after feedback from Henrik Tengelin.
It is much easier to read large numbers with with thousands separator but how to get them to work with page validation in a good way?
The solution in this case was to sprinkle some jQuery on the page!
Test how it works here
JavaScript code for adding thousands separator as watermark
$(function() { var numberInputs = $("input.number"); var convertToCurrencyDisplayFormat = function(str) { var regex = /(-?[0-9]+)([0-9]{3})/; str += ''; while (regex.test(str)) { str = str.replace(regex, '$1 $2'); } str += ' kr'; return str; }; var stripNonNumeric = function(str) { str += ''; str = str.replace(/[^0-9]/g, ''); return str; }; numberInputs.each(function() { this.value = convertToCurrencyDisplayFormat(this.value); }); numberInputs.blur(function() { this.value = convertToCurrencyDisplayFormat(this.value); }); numberInputs.focus(function() { this.value = stripNonNumeric(this.value); }); $("form").submit(function() { numberInputs.each(function() { this.value = stripNonNumeric(this.value); }); }); });
Tags: JavaScript, jQuery, Thousands Separator, tusentalsavgränsare, tusentalsseparator
-
-
I wrote an article about that for a while ago:
http://weblogs.asp.net/mikaelsoderstrom/archive/2008/11/06/bygg-ut-jquery.aspxGood luck!

![Fredrik Haglund [Photo by: Kristina Sahlén]](http://blog.fredrikhaglund.se/wp-content/uploads/2010/03/fredrik200.jpg)

6 comments
Comments feed for this article
Trackback link: http://blog.fredrikhaglund.se/blog/2010/03/05/using-jquery-to-add-thousands-separator-to-textbox/trackback/