如何使用 jquery 或 javascript 在文本框中只允许英文、数字和特殊字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12844477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to allow only english,numeric and special chars in textbox using jquery or javascript?
提问by Ram
I want to allow only English,numeric and special characters to be typed in my web page. i want to apply this thing using jquery or javascript. Actually my application is in 2 languages so for that i want to do this. I want to do the same thing with Arabic language too.. please help me. How can I do that?
我想只允许在我的网页中输入英文、数字和特殊字符。我想使用 jquery 或 javascript 应用这个东西。实际上,我的应用程序有 2 种语言,因此我想这样做。我也想用阿拉伯语做同样的事情..请帮帮我。我怎样才能做到这一点?
采纳答案by Anirudha
if ch is that char,you can do this
如果 ch 是那个字符,你可以这样做
if((ch.charCodeAt(0)>="a".charCodeAt(0) && ch.charCodeAt(0)<="z".charCodeAt(0))||(ch.charCodeAt(0)>="A".charCodeAt(0) && ch.charCodeAt(0)<="Z".charCodeAt(0)))
You can do the same comparison with arabic character
你可以用阿拉伯字符做同样的比较
which is represented in unicode
用 unicode 表示
[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]
回答by Leniel Maccaferri
You can try the following code that uses JavaScript replace method. The replace method accepts a regex pattern and so you can define pretty much anything you want/don't want typed in the textbox:
您可以尝试以下使用JavaScript 替换方法的代码。replace 方法接受正则表达式模式,因此您可以定义几乎任何您想要/不想在文本框中键入的内容:
<script type="text/javascript">
$('#textboxID').bind('keyup blur',function() {
$(this).val($(this).val().replace(/[^A-Za-z0-9]/g,''))
});
</script>
Here's the jsFiddle to try this out: http://jsfiddle.net/leniel/rtE54/
这是 jsFiddle 来试试这个:http: //jsfiddle.net/leniel/rtE54/
After a bit more thinking I implemented this code:
经过一番思考,我实现了这段代码:
$("#mytextbox").on("keypress", function(event) {
// Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase, digits 0 to 9 and white space)
// For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
var englishAlphabetDigitsAndWhiteSpace = /[A-Za-z0-9 ]/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
// englishAlphabetDigitsAndWhiteSpace.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 || englishAlphabetDigitsAndWhiteSpace.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();
});
You can read more about it here: JavaScript regex + jQuery to allow only English chars/letters in input textbox
您可以在此处阅读更多相关信息:JavaScript regex + jQuery to allow only English chars/letters in input textbox
回答by Aristos
You can limit the keys base on the onkeypress
checking if the key is in the limits that you set.
您可以根据onkeypress
检查密钥是否在您设置的限制范围内来限制密钥。
function ValidateKey()
{
var key=window.event.keyCode;
var allowed='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ :;,.?!£$%^&*()_+-*{}@~<>&"\'';
return allowed.indexOf(String.fromCharCode(key)) !=-1 ;
}
and you can use it as
你可以用它作为
<input size="30" value="" onkeypress="return ValidateKey();" >?
and on Fiddle: http://jsfiddle.net/UHGRz/3/
和小提琴:http: //jsfiddle.net/UHGRz/3/
You can applied to all input controls with jQuery. Did not work with copy/paste, there you need the Lenier solution with the Replace.
您可以使用 jQuery 应用于所有输入控件。不适用于复制/粘贴,您需要带有替换的 Lenier 解决方案。
And a convert to jQuery code
并转换为 jQuery 代码
jQuery("input").keypress(function()
{
var key=window.event.keyCode;
var allowed='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ :;,.?!£$%^&*()_+-*{}@~<>&"\'';
return allowed.indexOf(String.fromCharCode(key)) !=-1 ;
})?
and on Fiddle : http://jsfiddle.net/UHGRz/5/
和小提琴:http: //jsfiddle.net/UHGRz/5/
回答by Max
Try something like that. The regex for English characters is /^[A-Za-z0-9]*$/
尝试这样的事情。英文字符的正则表达式为 /^[A-Za-z0-9]*$/
<input name="yourtextbox" class="englishonly">
<script type="text/javascript">
$('.englishonly').bind('keyup blur',function(){
$(this).val( $(this).val().replace(/^[A-Za-z0-9]*$/g,'') ); }
);
</script>