gpt4 book ai didi

javascript - 如何允许除数字之外的所有字符?

转载 作者:行者123 更新时间:2023-11-29 16:34:15 26 4
gpt4 key购买 nike

我想允许除数字之外的所有字符。我在下面写了jquery。

$(".no-numbers").keypress(function(e){
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57))
{
swal({
title: "",
text: "Numbers are not allowed"
});

return false;
}
else
{
return true;
}
});

上面的代码不接受任何内容...请帮忙!!!

最佳答案

尝试以下操作:

$(".no-numbers").keypress(function(e){
var key = e.keyCode;
if (key >= 48 && key <= 57) {
swal({
title: "",
text: "Numbers are not allowed"
});

return false;
}
else
{
return true;
}
});

或者您可以使用正则表达式,例如:

var regex = new RegExp("^[a-zA-Z\s]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (regex.test(str)) {
// do your stuff here
}

关于javascript - 如何允许除数字之外的所有字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com