How to restrict "positive numbers" only as input to textbox (allow "-99" ONLY) using jQuery validator plugin?
Asked
Active
Viewed 2.5k times
4 Answers
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
- 63,341
- 64
- 307
- 441
-
2This helped me, but i also accepted tab key. `(event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 )` – Alper Karapınar Apr 16 '14 at 20:41
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
- 20,844
- 5
- 51
- 74

SaQiB
- 639
- 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,292
- 14
- 68
- 116
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
- 443
- 1
- 7
- 12