请问阻止了了大写字母但是还是可以输入汉字,并不是只输入数字和小写字母?
来源:4-19 事件对象(3)
TheRoo
2021-04-16 16:13:15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" name="" id="field">
<script>
var ofield=document.getElementById('field');
ofield.onkeypress = function(e){
console.log(e.charCode);
// 只能输入数字和小写字母
if(!(e.charCode>=48 && e.charCode<=57 || e.charCode>=96 && e.charCode<=122)){
e.preventDefault();
}
}
</script>
</body>
</html>
1回答
好帮手慕慕子
2021-04-16
同学你好,charCode获取的是键盘上对应的字符码,无法识别中文,所以无法限制中文输入;这里只要能实现阻止大写字母和数字即可。
祝学习愉快!
相似问题