javascript - Replacing comma with dot Char -
i have made calculation app in appjs.
basicly bunch of:
<input type=number>
fields.
to make more user friendly thought should replace commas dots, javascript can use actual values calculate.
i've tried doing this following pice of code:
$("input[type=number]").keyup(function(e){ var key = e.which ? e.which : event.keycode; if(key == 110 || key == 188){ e.preventdefault(); var value = $(this).val(); $(this).val(value.replace(",",".")); } });
in explorer 9, works expected: see fiddle
but since app.js uses chromium guess thats happens in chromium. how can work around this?
this happens in app: when enter number containing comma char. comma char moved right , when input box loses focus, comma removed (probably since comma char isn't allowed in type=number)
when value of <input type=number>
isn't valid, blank string returned. check doing this:
$("input[type=number]").keyup(function(e){ var key = e.which ? e.which : event.keycode; if(key == 110 || key == 188){ e.preventdefault(); var value = $(this).val(); console.log(value === ""); $(this).val(value.replace(",",".")); } });
it print true
every time. therefore, need to
since, on
keyup
event, input has changed, must changekeydown
orkeypress
event.change
value.replace(",", ".")
value + "."
(since there no","
).- actually, need insert cursor is. i'll update when have time.
finished code:
$("input[type=number]").keydown(function (e) { var key = e.which ? e.which : event.keycode; if (key == 110 || key == 188) { e.preventdefault(); var value = $(this).val(); console.log(value); $(this).val(value + "."); } });
a better idea might make <input type=text>
, validate manually if need feature.
Comments
Post a Comment