forms - JavaScript Code is not Letting me Input Commas -
i trying make loan calculator allows maximum of 2 decimals typed @ once. unfortunately, have had add script input disallows commas inputted. not sure why not allow commas. commas purpose of separating every third number. example, 1,000,000.
a working example of page can seen at: http://thetotempole.ca/sales/loancalculator.php
i not looking suggestions, full solutions problem, please.
here script using currently:
<script> function trimdp(x, dp) { x = parsefloat(x); if (dp === 0) return math.floor(x).tostring(); dp = math.pow(10, dp || 2); return (math.floor((x) * dp) / dp).tostring(); } window.addeventlistener('load', function () { var nodes = document.queryselectorall('.dp2'), i; function press(e) { var s = string.fromcharcode(e.keycode); if (s === '.') if (this.value.indexof('.') === -1) return; // permit typing `.` this.value = trimdp(this.value + s); e.preventdefault(); }; function change() { this.value = trimdp(this.value); } (i = 0; < nodes.length; ++i) { nodes[i].addeventlistener('keypress', press); nodes[i].addeventlistener('change', change); } }); </script>
and here input code:
<input type=text name=loan size=10 class="dp2">
you have couple of errors in there.
as have written comment. e.keycode
wont work on firefox, @ least not work on mine. instead, should do
var s = string.fromcharcode(e.keycode || e.charcode);
by doing e.preventdefault();
in keypress listener, disabling key presses, includes arrow keys, backspace, delete, etc.
i wont mention hundred time parsefloat
.
more on determining key pressed.
Comments
Post a Comment