Click here to Skip to main content
15,886,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have this text input that has to convent the value in the German price format on .blur(). It works when the user presses period for the decimal separator, but since I have a German keyboard I would like to press the comma to get the same results. I added a code snippet below for a demonstration. With this if I type 1545454.154 the input will result in 1.545.454,15 (thanks to the toLocalString DE). If I change the period with a comma in the line (/[^0-9\.]/g, ''); to (/[^0-9\,]/g, '') I can now press my comma but the result will be 1.545.454,00. So the code is not seeing my comma as a separator. I know I should use .replace(",",".") but I do not know where to put it. Thanks in advance for any help.

$(window).ready(function() {
   function validateNumber(event) {
     this.value = this.value.replace(/[^0-9\.]/g, '');
   };
   $('#int').keyup(validateNumber);
   $('#float1').keyup(validateNumber);
   $('#float1').on("blur", formatDecimal);

   function formatDecimal(event) {
     console.log($(this).attr('decimalplaces'));
     var number = parseFloat(this.value);
     this.value = isNaN(number) ? '0' : number.toLocaleString('de-DE', {
       maximumFractionDigits: 2,
       style: 'currency',
       currency: 'EUR'
     });
   };

   $('#float2').keyup(validateNumber);
   $('#float2').on("blur", formatDecimal);
   var dt = dateFormatter('#date');
   $('[id^=date]').keypress(validateNumber);
 });
Posted
Comments
Afzaal Ahmad Zeeshan 17-Jun-15 11:08am    
Can you share what is your expected output?
Richard Deeming 17-Jun-15 11:09am    
Sergey Alexandrovich Kryukov 17-Jun-15 15:09pm    
Would you post it as a formal answer, perhaps with few more words? It's very likely what the inquirer really needs.
—SA
Maciej Los 17-Jun-15 15:39pm    
Agree!
Richard Deeming 17-Jun-15 16:12pm    
Done. :)

Internationalization and localization of javascript code isn't particularly easy. The parseFloat method doesn't handle different culture settings; it always assumes that "." is the decimal separator, and "," is the thousands separator.

The options you're passing to the toLocaleString method[^] are only supported in modern browsers - Chrome 24+, Firefox 29+, IE11+, and Opera 15+. They are not supported in Safari.

The jQuery Foundation have developed a well-supported, open-source library to handle these problems: https://github.com/jquery/globalize[^]

Using the library will let you parse and format numbers, currency, dates, times, and other common items using your preferred locale. For example:
JavaScript
var number = Globalize("de-DE").parseNumber(this.value);
this.value = Globalize("de-DE").formatCurrency(number, "EUR", { maximumFractionDigits: 2 });
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Jun-15 16:15pm    
My voted of 5 — done. :-)
—SA
Daria90 18-Jun-15 9:43am    
Hello Richard, thank you for your time to search that for me. I tried this whole morning to make that work but installing it and implementing the library in my HTML was impossible for me. I was thinking, toLocalString event works, I just need to figure a way to make the code think that when I press the comma padkey(with a keyCode of 44 on my keyboards) I actually pressed the period key. Because when I press the period key for my decimals it makes the formatting fine and even converts the period into a comma. Any idea how I can do that?
Here you guys, I made this JsFiddle : http://jsfiddle.net/Daria90/zuxu5tva/2/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900