jQuery JS函数只允许输入字母和空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19849189/
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
JS function to allow enter only letters and white spaces
提问by user2683519
I need a jquery or js function to only allow enter letters and white spaces. Thanks in advance.
我需要一个 jquery 或 js 函数来只允许输入字母和空格。提前致谢。
page:
页:
<p:inputText onkeypress="onlyLetter(this)">
function:
功能:
function onlyLetter(input){
$(input).keypress(function(ev) {
var keyCode = window.event ? ev.keyCode : ev.which;
// code
});
}
回答by Md. Ashaduzzaman
Note: KeyboardEvent.whichis deprecated as of Jan. 1, 2020
注意:KeyboardEvent.which 自2020 年 1 月 1 日起已弃用
Just use ascii codes (decimal values) of keys/digits that you want to disable or prevent from being work. ASCII Table.
只需使用要禁用或阻止工作的键/数字的 ascii 代码(十进制值)。ASCII 表。
HTML :
HTML :
<input id="inputTextBox" type="text" />
jQuery :
jQuery :
$(document).ready(function(){
$("#inputTextBox").keydown(function(event){
var inputValue = event.which;
// allow letters and whitespaces only.
if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
});
});
回答by VIJAY P
The following code allows only a-z, A-Z, and white space.
以下代码仅允许使用 az、AZ 和空格。
HTML
HTML
<input id="inputTextBox" type="text" />
jQuery
jQuery
$(document).on('keypress', '#inputTextBox', function (event) {
var regex = new RegExp("^[a-zA-Z ]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
回答by markasoftware
First off, I have little experience in jQuery and will provide a vanilla javascript example. Here it is:
首先,我对 jQuery 几乎没有经验,将提供一个普通的 javascript 示例。这里是:
document.getElementById('inputid').onkeypress=function(e){
if(!(/[a-z ]/i.test(String.fromCharCode(e.keyCode))) {
e.preventDefault();
return false;
}
}
回答by taji01
Tweaking Ashad Shanto answer a bit. Notice you cant type in y and z if you use the script. You have to change the inputValue from 120 to 123. Here is the ASCII table reference: http://ee.hawaii.edu/~tep/EE160/Book/chap4/subsection2.1.1.1.htmlUse the script below to type in all the letters, space and backspace.
稍微调整了 Ashad Shanto 的回答。请注意,如果您使用脚本,则不能输入 y 和 z。您必须将 inputValue 从 120 更改为 123。这是 ASCII 表参考:http://ee.hawaii.edu/~tep/EE160/Book/chap4/subsection2.1.1.1.html使用下面的脚本键入在所有字母,空格和退格。
<script>
$(document).ready(function(){
$("#inputTextBox").keypress(function(event){
var inputValue = event.which;
// allow letters and whitespaces only.
if(!(inputValue >= 65 && inputValue <= 123) && (inputValue != 32 && inputValue != 0)) {
event.preventDefault();
}
console.log(inputValue);
});
});
</script>
回答by Jesus Quevedo
回答by Vikas
<!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>
<body>
<br/>
Enter Only Alphabate: <input id="txtName" name="lname">
<script>
$('#txtName').keypress(function (e) {
var regex = new RegExp("^[a-zA-Z \s]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
return true;
}
else
{
e.preventDefault();
alert('Please Enter Alphabate');
return false;
}
});
</script>
</body>
</html>
回答by Sathish Swaminathan
Here is the code which you can understand easily and can modify for any character char
exception.
这是您可以轻松理解并且可以针对任何字符char
异常进行修改的代码。
I include the exception for BACKSPACE
.
我包括BACKSPACE
.
Likewise you can give the exception by including the keycode inside the statement.
同样,您可以通过在语句中包含键码来给出异常。
var c= ((e.which>=65 && e.which<91) || (e.which==8 /**Here 8 if for the Backspace**/) || (e.which=="your key code"))
https://gist.github.com/SathishSaminathan/e3c509243ead20fcae26c87fdd6f78fd
https://gist.github.com/SathishSaminathan/e3c509243ead20fcae26c87fdd6f78fd
回答by Packiyaraj Shanmugam
function ValidateAlpha(evt) {
var keyCode = (evt.which) ? evt.which : evt.keyCode if (
(keyCode < 65 || keyCode > 90) &&
(keyCode < 97 || keyCode > 123) &&
keyCode != 32 &&
keyCode != 39
)