Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to restrict the desired range in the numeric box?
Posted

Try with below code:
JavaScript
// To allow only numbers
$('#inputTxt').keydown(function (e) {
	if (e.shiftKey || e.ctrlKey || e.altKey) {
		e.preventDefault();
	} else {
	var key = e.keyCode;
		if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57)      || (key >= 96 && key <= 105))) {
			e.preventDefault();
		}
	}
});

// To allow value within range between 50 to 100
$("#inputTxt").on("change", function() {
    var val = parseInt(this.value);
    if(val > 100 || val < 50)
    {
        alert('Wrong range');
        this.value ='';        
    }
})

Here inputTxt is ID of text box. You can change as per your need.
 
Share this answer
 
v2
Comments
Abhinav S 8-Oct-15 1:59am    
5.
Try something like
$('input').keyup( function (event) {
   if (i$(this).val() < x ) || ($(input).val() > y) event.preventDefault(); 
}
 
Share this answer
 
v2
Comments
Krunal Rohit 8-Oct-15 1:55am    
you forgot the event.
$('input').keyup( function (event) {
if (i$(this).val() < x ) || ($(input).val() > y) event.preventDefault();
}

-KR
Abhinav S 8-Oct-15 1:59am    
Thanks. Updated.
HTML
<input type="number" min="0" max="10" value="3"></input>

Or
clean JavaScript solution,
Demo here[^]

-KR
 
Share this answer
 
Comments
Abhinav S 8-Oct-15 1:59am    
Another approach. 5.
Krunal Rohit 8-Oct-15 2:05am    
Thanks :)

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900