我想禁止用户将字符输入到不是字母或数字的HTML文本输入字段中.
这个确切的功能可以在Twitter注册页面(用户名字段)https://twitter.com/signup上看到.
我试图在jQuery中写这个.但我需要能够获取incomming keypress事件值来测试按下了哪个键以及它的值是否可接受.
这是代码:
<input type="text" id="username" name="username" />
<script type="text/javascript">
$("#username").keypress(function(event){
// Get the keypress value
// ...?
// If the keypress value is illegal then disable it
if (...){
event.preventDefault();
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
Vin*_*ini 33
您可以使用事件对象的"which"属性来捕获键代码.您可以使用类似以下代码的内容来获取相应的字符 -
var c = String.fromCharCode(e.which);
有关详细说明,请参阅http://api.jquery.com/keypress/链接.
| 归档时间: |
|
| 查看次数: |
35907 次 |
| 最近记录: |