如何在jquery中过滤只接受英文字母字符的文本框?
我有一个文本框,我想用jquery中的英文字母字符对其进行过滤。有什么帮助吗?试试:如何在jquery中过滤只接受英文字母字符的文本框?,jquery,Jquery,我有一个文本框,我想用jquery中的英文字母字符对其进行过滤。有什么帮助吗?试试: $("#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 Expression
$("#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();
});
尝试:
$('.acceptalpha').bind('keyup blur',function()
{
var thisttext=$(本);
thistText.val(thistText.val().replace(/[^a-z]/g',);
});代码>
$('.acceptalpha').bind('keyup blur',function()
{
var thisttext=$(本);
thistText.val(thistText.val().replace(/[^a-z]/g',);
});代码>
可能重复@ShabbirDhangot,这是不同的little@MohsenZahedi您尝试的源代码在哪里?可能是@ShabbirDhangot的副本,它是不同的little@MohsenZahedi您尝试的源代码在哪里?@Mohsend如果成功,您可以将其标记为已完成you@Mohsend您可以将其标记为已完成,如果它已在你