1

How to restrict "positive numbers" only as input to textbox (allow "-99" ONLY) using jQuery validator plugin?

cspolton
  • 4,435
  • 4
  • 25
  • 34
reshma k
  • 534
  • 1
  • 3
  • 11

4 Answers4

5

Without using any Plugin you can easily do that by using below mentioned code.

HTML

<input type="text" name="returnRetailQuantity" id="returnRetailQuantity" />

jQuery Code

//for allow only positive integers
$(document).ready(function(){

        $("#returnRetailQuantity").keydown(function (event) {
            if (event.shiftKey) {
                event.preventDefault();
            }

            if (event.keyCode == 46 || event.keyCode == 8) {
            }
            else {
                if (event.keyCode < 95) {
                    if (event.keyCode < 48 || event.keyCode > 57) {
                        event.preventDefault();
                    }
                }
                else {
                    if (event.keyCode < 96 || event.keyCode > 105) {
                        event.preventDefault();
                    }
                }
            }
        });
});

Hope this will help you.

Sampath
  • 58,546
  • 53
  • 279
  • 406
4

You can use jquery-numeric.

See also: jQuery: what is the best way to restrict "number"-only input for textboxes? (allow decimal points).

Adam Millerchip
  • 16,151
  • 5
  • 41
  • 56
SaQiB
  • 619
  • 8
  • 18
2

The easiest that I've used is http://digitalbush.com/projects/masked-input-plugin/.

There is a simple javascript function at http://michael.theirwinfamily.net/articles/jquery/creating-input-fields-only-accept-integers-jquery

photo_tom
  • 7,232
  • 13
  • 66
  • 115
0
<input type="number" id="num" name="anyname" min='0' />
<script>
$(document).ready(function()
    {
var num = $('#num').val();
$('#num').blur(function()
    {
        if(num<0)
        {
            if(num==-99)
            {
                alert('Valid number');
            }
            else
            {
                alert('Invalid number');
            }
        }
        else
        {
            alert('Valid number');
        }
    });
});
</script>
w.Daya
  • 196
  • 1
  • 3
  • 10