83
rated 0 times
[
89]
[
6]
/ answers: 1 / hits: 17811
/ 9 Years ago, thu, september 10, 2015, 12:00:00
My requirement is to not allow user to type in any Alphabets. The below code allows 1 character to be entered even though I have provided the e.preventDefault()
method on both keydown
and keyup
methods.
$(function() {
// Regular Expression to Check for Alphabets.
var regExp = new RegExp('[a-zA-Z]');
$('#test').on('keydown keyup', function(e) {
var value = $(this).val();
// Do not allow alphabets to be entered.
if (regExp.test(value)) {
e.preventDefault();
return false;
}
}); // End of 'keydown keyup' method.
}); // End of 'document ready'
<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
<input type=text id=test name=test />
What am I doing wrong? Is there some other way to get this done?
More From » jquery