I have a textbox and I want filter it by english alphabetic characters In jquery.any help ?
Asked
Active
Viewed 993 times
0
-
Possible duplicate of [filter jquery results based on name](http://stackoverflow.com/questions/33635607/filter-jquery-results-based-on-name) – Shabbir Dhangot Jun 20 '16 at 07:15
-
1– Divyesh Patoriya Jun 20 '16 at 07:16
-
@ShabbirDhangot it's diffrent a little – Mohsen Zahedi Jun 20 '16 at 07:25
-
@MohsenZahedi where is the *source code* of your attempt? – Patrick Trentin Jun 20 '16 at 07:30
2 Answers
2
$('.acceptalpha').bind('keyup blur',function()
{
var thistext = $(this);
thistext.val(thistext.val().replace(/[^a-z]/g,'') );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="text" id="acceptalpha" class="acceptalpha">

Divyesh Patoriya
- 518
- 3
- 15
1
Try:
$("#mytextbox").on("keypress", function(event) {
// Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
// For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;
// Retrieving the key from the char code passed in event.which
// For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
var key = String.fromCharCode(event.which);
//alert(event.keyCode);
// For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
// keyCode == 8 is backspace
// keyCode == 37 is left arrow
// keyCode == 39 is right arrow
// englishAlphabetAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
return true;
}
// If we got this far, just return false because a disallowed key was typed.
return false;
});
$('#mytextbox').on("paste",function(e)
{
e.preventDefault();
});

Jayesh Chitroda
- 4,987
- 13
- 18