0

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>
Community
  • 1
  • 1
Peon
  • 7,216
  • 7
  • 45
  • 87
  • First of all why do they both have different classes? You could just give them both the same class, `numeric`, then call `$(".numeric").numeric()`. If they need to be different classes, consider having another class in common with both of them. – ClarkeyBoy Jul 31 '12 at 07:16
  • I wasn't sure if they would work with the same class, so I renamed them. But that's not the issue anyways. – Peon Jul 31 '12 at 07:17
  • 2
    See [this](https://github.com/johnedmonds/jQuery-Plugins/commit/fc0fde93d8d595c22ff0af545a63c32f1fff936c) from numeric – Alex Ball Jul 31 '12 at 07:28
  • Just deleted my answer, if it's there don't reinvite it. @AlexBall you should post your link as an answer. – David Hedlund Jul 31 '12 at 07:36
  • Noticed your answer before it got deleted. Worked perfect :) – Peon Jul 31 '12 at 07:38
  • @DavidHedlund If your answer work (like mentioned by Dainis ), resend it, and, if you want, add my link in comment like other resource. ;-) – Alex Ball Jul 31 '12 at 07:40

1 Answers1

1

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:

var decimalIx = $(this).val().indexOf(decimal);
allow = decimalIx == -1 || decimalIx >= $(this).val().length - 2;

I was going to make the -2 configurable, but as Alex Ball pointed out in comments, the work has already been done.

David Hedlund
  • 121,697
  • 28
  • 196
  • 213