Javascript 如何允许除数字以外的所有字符?

Javascript 如何允许除数字以外的所有字符?,javascript,jquery,Javascript,Jquery,我想允许除数字以外的所有字符。我已经在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 f

我想允许除数字以外的所有字符。我已经在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 number”)。按键(功能(e){
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57))
{
游泳({
标题:“,
文本:“不允许使用数字”
});
返回false;
}
其他的
{
返回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;
  }
}); 
参考文献

您可以将按键转换为实际值,然后使用e.preventDefault()忽略输入

$(“.no number”)。按键(功能(e){
变量数=['0','1','2','3','4','5','6','7','8','9'];
var-keynum;
if(window.event){//IE
keynum=e.keyCode;
}否则,如果(如){//Netscape/Firefox/Opera
keynum=e.which;
}
if(numbers.indexOf(String.fromCharCode(keynum))>-1){
警惕(“请不要打电话”);
e、 预防默认值();
}
});

看起来您的if语句是错误的

看看这个沙箱


如果(e.which>=48&&e.which您的条件是错误的

你必须测试48到57之间的键码,以及96到105之间的键盘号码

你原来的情况说明:

(e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) 
(e.which!=8&&e.which!=0&&e.which<48|e.which>57))
如果键不是退格键(8)

如果密钥不是…则没有密钥代码零!!

如果该键低于键码48或高于键码57-->则与您所希望的相反


现在看下面的代码片段;)

$(“.no number”).keydown(函数(e){

如果((e.which>=48&&e.which=96&&e.which),为什么不使用正则表达式只允许字符?
if (e.which >= 48 && e.which <= 57) {
  console.log("Not accepted is a  Numbers");
  return false;
} else {
  console.log("Accepted not a number");
  return true;
}
(e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57))