[英]When I try to validate the jsp form on keypress event, the alert box appears before entering any data into an input field
When I try to validate the jsp form on keypress event, the alert box appears before entering any data into an input field当我尝试在按键事件上验证 jsp 表单时,在将任何数据输入到输入字段之前会出现警告框
<html>
<head>
<script text/javascript>
var inputs = document.getElementsByTagName("input");
function checkTabPress(e)
{
for(var i=0;i<inputs.length;i++)
{
if((inputs[i].value === undefined || inputs[i].value.length == 0) && (e.keyCode == 9))
{
alert("plz write");
return false;
}
}
return true;
}
document.addEventListener('keyup', function (e) {
checkTabPress(e);
}, false);
</script>
</head>
<body>
Enter Your Name: <input onkeypress="checkTabPress('abc')" type='text' id="abc" placeholder=''><br>
Enter <input onkeypress="checkTabPress('xyz')" type='text' id="xyz" placeholder=''>
</body>
</html>
You need to change your onkeypress
event to onblur
(of the text inputs) and there is no need of addEventListener for keypress
您需要将
onkeypress
事件更改为onblur
(文本输入的),并且不需要为keypress
Updated DEMO JSFiddle URL : https://jsfiddle.net/n6j7oy3b/3/更新了演示 JSFiddle URL : https : //jsfiddle.net/n6j7oy3b/3/
Hope this helps!希望这有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.