Im building web app which is mainly for mobile browsers. Im using input fields with number type, so (most) mobile browsers invokes only number keyboard for better user experience. This web app is mainly used in regions where decimal separator is comma, not dot, so I need to handle both decimal separators.
How to cover this whole mess with dot and comma?
My findings:
Desktop Chrome
- Input type=number
- User enters "4,55" to input field
$("#my_input").val();
returns "455"- I can not get the correct value from input
Desktop Firefox
- Input type=number
- User enters "4,55" to input field
$("#my_input").val();
returns "4,55"- Thats fine, I can replace comma with dot and get correct float
Android browser
- Input type=number
- User enters "4,55" to input field
- When input loses focus, value is truncated to "4"
- Confusing to user
Windows Phone 8
- Input type=number
- User enters "4,55" to input field
$("#my_input").val();
returns "4,55"- Thats fine, I can replace comma with dot and get correct float
What are the "best practices" in this kind of situations when user might use comma or dot as decimal separator and I want to keep html input type as number, to provide better user experience?
Can I convert comma to dot "on the fly", binding key events, is it working with number inputs?
EDIT
Currenlty I do not have any solution, how to get float value (as string or number) from input which type is set to number. If enduser enters "4,55", Chrome returns always "455", Firefox returns "4,55" which is fine.
Also it is quite annoying that in Android (tested 4.2 emulator), when I enter "4,55" to input field and change focus to somewhere else, the entered number get truncated to "4".
.val()
, and Android does something weird. Can you check if they behave the same when you set the system language to something appropriate? – millimoosevar number = $(...).get(0).valueAsNumber;
if (isNaN(number)) number = parseFloat($(...).val().replace(',', '.');
But that solution only works if the number that has been put in only contains 1 comma and no extra dots... – bert bruynooghe