使用 jquery/javascript 检测数字或字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2257070/
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
Detect numbers or letters with jquery/javascript?
提问by ajsie
I want to use an if-statement to run code only if the user types in a letter or a number.
仅当用户输入字母或数字时,我才想使用 if 语句来运行代码。
I could use
我可以用
if(event.keyCode == 48 || event.keyCode == 49 || event.keyCode == 50..) {
// run code
}
Is there an easier way to do this? Maybe some keycodes don't work in all web browsers?
有没有更简单的方法来做到这一点?也许某些键码不适用于所有网络浏览器?
回答by Andy E
If you want to check a range of letters you can use greater than and less than:
如果要检查一系列字母,可以使用大于和小于:
if (event.keyCode >= 48 && event.keyCode <= 57)
alert("input was 0-9");
if (event.keyCode >= 65 && event.keyCode <= 90)
alert("input was a-z");
For a more dynamic check, use a regular expression:
要进行更动态的检查,请使用正则表达式:
var inp = String.fromCharCode(event.keyCode);
if (/[a-zA-Z0-9-_ ]/.test(inp))
alert("input was a letter, number, hyphen, underscore or space");
See the MDC documentationfor the keyCodeproperty, which explains the difference between that and the whichproperty and which events they apply to.
请参阅MDC文档的keyCode属性,这解释了和之间的差异which性,这事件,他们申请。
回答by Tim Down
First, if you're doing this, make sure it's in the keypressevent, which is the only event for which you can reliably obtain information about the character the user has typed. Then I'd use the approach Andy E suggested:
首先,如果您正在执行此操作,请确保它在keypress事件中,这是您可以可靠地获取有关用户键入的字符的信息的唯一事件。然后我会使用 Andy E 建议的方法:
document.onkeypress = function(evt) {
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
var charStr = String.fromCharCode(charCode);
if (/[a-z0-9]/i.test(charStr)) {
alert("Letter or number typed");
}
};
If you want to check for backspace, I'd use the keydownevent instead and check for a keyCodeof 8 because several browsers (including Chrome) do not fire a keypressevent for the backspace key.
如果您想检查退格键,我会改用该keydown事件并检查 a 是否为keyCode8,因为一些浏览器(包括 Chrome)不会keypress为退格键触发事件。
回答by Gibolt
Use event.keyand modern JS!
使用event.key和现代JS!
No number codesanymore. You can check key directly.
没有数字代码了。您可以直接检查密钥。
const key = event.key.toLowerCase();
if (key.length !== 1) {
return;
}
const isLetter = (key >= "a" && key <= "z");
const isNumber = (key >= "0" && key <= "9");
if (isLetter || isNumber) {
// Do something
}
You could also use a simple regex. ^$ensures 1 char, iignores case
您也可以使用简单的正则表达式。^$确保 1 个字符,i忽略大小写
/^[a-z0-9]$/i.test(event.key)
or individually:
或单独:
const isLetter = /^[a-z]$/i.test(event.key)
const isNumber = /^[0-9]$/i.test(event.key)
回答by Douwe Maan
if(event.keyCode >= 48 && event.keyCode <= 90) {
//the key pressed was alphanumeric
}
回答by MD Sulaiman
For Numeric Values:
对于数值:
function ValidNumeric()
{
var charCode = (event.which) ? event.which:event.KeyCode;
if (charCode>=48 && charCode<=57)
{
return true;
}
else
return false;
}
Here, 48 and 57 is the range of numeric values.
这里,48 和 57 是数值的范围。
For Alphabetic:
对于字母:
function ValidAplpha()
{
var charCode = (event.which) ? event.which:event.KeyCode;
if(charCode >= 65 && charCode <= 90 || charCode>=97 && charCode<=122)
{
return true;
}
else
return false;
}
Here, 65 to 90 is the range for Capital alphabates (A-Z) and 97 to 122 is range for small alphabates (a-z)
这里,65 到 90 是大写字母 (AZ) 的范围,97 到 122 是小字母 (az) 的范围
回答by Fede Scuoteguazza
As @Gibolt said, you should Use event.key
正如@Gibolt 所说,你应该使用 event.key
Because charCode, keyCode and Which are being deprecated.
因为 charCode、keyCode 和 which 已被弃用。
回答by Fede Scuoteguazza
number validation, works fine for me
号码验证,对我来说很好用
$(document).ready(function () {
$(".TxtPhone").keypress(function (e) {
var key = e.charCode || e.keyCode || 0;
// only numbers
if (key < 48 || key > 58) {
return false;
}
});
});
回答by alex_P
You can also use charCodewith onKeyPressevent:
您还可以将charCode与onKeyPress事件一起使用:
if (event.charCode > 57 || event.charCode < 48) {
itsNotANumber();
}
else {
itsANumber();
}
回答by Donnie Gallocanta Jr.
use $.isNumeric(value);return type is boolean
使用$.isNumeric(value);返回类型是布尔值
回答by Bharat Kumar
Accept numbers or letters with javascript by Dynamic Process using regular Expression.
使用正则表达式通过动态过程接受带有 javascript 的数字或字母。
Add onkeypress Event for Specific Control
为特定控件添加 onkeypress 事件
onkeypress="javascript:return isNumber(event)"
onkeypress="javascript:return isNumber(event)"
function numOnly(evt) {
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
var charStr = String.fromCharCode(charCode);
if (/[0-9]/i.test(charStr)) {
return true;
}
else
return false;
}
function Alphanum(evt) {
evt = evt || window.event;
var charCode = evt.which || evt.keyCode;
var charStr = String.fromCharCode(charCode);
if (/[a-z0-9]/i.test(charStr)) {
return true;
}
else
return false;
}

