Home » » How To Prevent User From Entering A Non-number In Textbox Using Jquery?

How To Prevent User From Entering A Non-number In Textbox Using Jquery?

Rendyon | 12:02 AM | 0 comments

Suppose you have requirement to validate mobile number, if the user types alphabets/special character, it doesn't allow the character to enter in the textbox(it only allow numbers). Also, it allow backspace to delete numbers which you have entered and it also allow to select all(using ctrl+A) and then delete.

Here is HTML code:
 


Here is the JQuery code:
$("#mobNumber").keydown(function (e) {
var id = $(this).attr('id');
$("#"+id).keypress(function(e) {

if ( (e.ctrlKey) || (e.which >= 48 && e.which <= 57) || (e.which == 95) || (e.which == 8) || (e.which == 32) || (e.which == 0)) {
}
else
{
e.preventDefault();
}
});

});

Check running example here: http://jsfiddle.net/jeetu_verma11/kLP2n/
Share this article :

0 comments:

 
Copyright © 2011. Find Updates - All Rights Reserved
Template Modify by Creating Website
Proudly powered by Blogger