Textbox accept only numbers (digits) using jquery

Textbox accept only numbers (digits) using jquery

by Shuaib Khan

HTML

Number : <input type="number" min="0" max="1050" name="quantity" id="quantity" />&nbsp;<span id="errmsg"></span>

CSS

#errmsg
{
color: red;
}

JavaScript

$(document).ready(function () {
var maxVal = 1050;
  //called when key is pressed in textbox
  $("#quantity").keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        $("#errmsg").html("Digits Only").show().fadeOut("slow");
               return false;
    	}else{
			/* 	if(parseInt(e.target.value) >= maxVal){
					$("#quantity").val(maxVal)
				} */
			}
   });
});