Possible Duplicate:
JavaScript: formatting number with exactly two decimals
I need a JS (jQuery) script, that helps me limit the input of numbers to two decimal points.
I have tried this and it works perfect limiting my input only to floats or integers. But I can’t understand how to make it limit to two decimal points.
I also looked at lots of other answers here, but none seem to stop the user from inputing more then two digits while typing.
Any suggestions? Thanx.
EDIT: My code, if it helps
The form:
<form method="post" id="split_form"
action="?name=ajax&case=split_action&sid=' . $id . '"
onSubmit="return checkform(this);" >
<table class="default" cellpadding="0" cellspacing="0">
<tr><td><input class="numeric1" type="text" name="summ_1" value="" />
<tr><td><input class="numeric2" type="text" name="summ_2" value="" />
</table>
<input type="submit" class="submit" value="ok" />
</form>
JS scripts:
<script type="text/javascript" src="/js/jquery.numeric.js"></script>
<script language="JavaScript" type="text/javascript">
$(".numeric1").numeric();
$(".numeric2").numeric();
function checkform ( form ) {
var sum1 = form.summ_1.value;
var sum2 = form.summ_2.value;
var new_summ = parseFloat(sum1) + parseFloat(sum2);
var summ = parseFloat(' . $row[1]['summ'] . ');
if ( ( sum1 > 0 && sum2 > 0 ) && ( new_summ == summ ) ) {
return true;
} else {
return false ;
}
}
</script>
In the numeric plugin, line 135, numeric entries are treated. Currently, they are always allowed. You could modify this plugin to allow numbers only if the decimal point is nonexistent, or no more than two steps away:
I was going to make the
-2configurable, but as Alex Ball pointed out in comments, the work has already been done.