2015-04-14 177 views
0

我呼吁keypress功能,其中不允许字母值,但允许有进,退格数字字符,删除,箭头等不允许字母值,但允许进入,退格键,删除,箭头等

function checkIt(s) { 
    if (s.match('/[^a-zA-Z0-9 ]/g')) { 
    s = s.replace('/[^a-zA-Z0-9 ]/g', ''); 
    } 
} 

HTML

<input type="text" onkeyup="return checkIt(this.value)" maxlength="4" class="input-layout" value="10" name="test[]"> 
<input type="text" onkeyup="return checkIt(this.value)" maxlength="4" class="input-layout" value="10" name="test[]"> 
<input type="text" onkeyup="return checkIt(this.value)" maxlength="4" class="input-layout" value="10" name="test[]"> 
<input type="text" onkeyup="return checkIt(this.value)" maxlength="4" class="input-layout" value="10" name="test[]"> 

此代码无法使用。

+1

这是应该做的。该函数实际上不会返回值或更改任何内容,因为它正在影响局部变量。 – DGS

回答

2

你可以尝试这样的事情

$('textarea').keyup(function() { 
 
    $(this).val(function(i, v) { 
 
    return v.replace(/[a-z]/gi, ''); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<textarea></textarea>

,或者你可以做一些事情event.keyCode

$('textarea').keypress(function(e) { 
 
    if ((e.keyCode < 91 && e.keyCode > 64) || (e.keyCode < 123 && e.keyCode > 96)) 
 
    return false; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<textarea></textarea>

或使用代码

function checkIt(s) { 
 
    s.value=s.value.replace(/[a-z]/gi,''); 
 
}
<input type="text" onkeyup="checkIt(this)" maxlength="4" class="input-layout" value="10" name="test[]"> 
 
<input type="text" onkeyup="checkIt(this)" maxlength="4" class="input-layout" value="10" name="test[]"> 
 
<input type="text" onkeyup="checkIt(this)" maxlength="4" class="input-layout" value="10" name="test[]"> 
 
<input type="text" onkeyup="checkIt(this)" maxlength="4" class="input-layout" value="10" name="test[]">

+0

您可以使用此功能提供解决方案吗?你可以提供 –

+0

的html代码吗? –

+0

已在上面添加。 pl检查 –

0

几件事情需要注意:

  1. 正则表达式不应该放在引号中
  2. 放置一个插入符号意味着你不要它。既然你想要数字而不是字母表,你不能像这样把它们放在一起。
  3. 你的函数应该返回值。

答案已经由别人在这里提供,但我想你应该知道这些观点。

+0

我想使用此功能,我试图删除报价,但它仍然不工作 –

相关问题