为了账号安全,请及时绑定邮箱和手机立即绑定

输入字段不能输入其他字符

输入字段不能输入其他字符

绝地无双 2022-10-13 10:40:16
我可以知道是什么错误导致我的输入字段无法输入字符吗?因为在输入字段中设置></\":*?|了这些符号限制后,其他字符无法在输入字段中输入。<!DOCTYPE html><html><body><h1>Can't type character in the input field</h1><input type="text" class="form-control blank" id="function_code" name="function_code" title="function_code" onpaste="return false"><div id="error-box"></div></body></html><script>function showError (key) {  var errBox = document.querySelector("#error-box");  errBox.textContent = "The character " + key.toString() + " is not allowed!";  //Dismiss the error  window.setTimeout(function () {      errBox.textContent = "";  }, 10000)}document.getElementById("function_code").onkeypress = function(e) {var chr = String.fromCharCode(e.which);if ("></\":*?|".indexOf(chr) >= 0)  showError(chr)  return false;};</script>希望有人能帮我解决这个问题。谢谢。
查看完整描述

2 回答

?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

if ("></\":*?|".indexOf(chr) >= 0){

  showError(chr)

   return false;

}

return true // !!!

<!DOCTYPE html>

<html>

<body>


<h1>Can't type character in the input field</h1>



<input type="text" class="form-control blank" id="function_code" name="function_code" title="function_code" onpaste="return false">

<div id="error-box"></div>


</body>

</html>


<script>


function showError (key) {

  var errBox = document.querySelector("#error-box");

  errBox.textContent = "The character " + key.toString() + " is not allowed!";

  //Dismiss the error

  window.setTimeout(function () {

      errBox.textContent = "";

  }, 10000)

}



document.getElementById("function_code").onkeypress = function(e) {

var chr = String.fromCharCode(e.which);


if ("></\":*?|".indexOf(chr) >= 0){

  showError(chr)

   return false;

}

return true

};

</script>


查看完整回答
反对 回复 2022-10-13
?
隔江千里

TA贡献1906条经验 获得超10个赞

只需删除 return false; 或将其更改为 true


如果您返回 false,则不会输入任何内容


<!DOCTYPE html>

<html>

<body>


<h1>Can't type character in the input field</h1>



<input type="text" class="form-control blank" id="function_code" name="function_code" title="function_code" onpaste="return false">

<div id="error-box"></div>


</body>

</html>


<script>


function showError (key) {

  var errBox = document.querySelector("#error-box");

  errBox.textContent = "The character " + key.toString() + " is not allowed!";

  //Dismiss the error

  window.setTimeout(function () {

      errBox.textContent = "";

  }, 10000)

}



document.getElementById("function_code").onkeypress = function(e) {

var chr = String.fromCharCode(e.which);


if ("></\":*?|".indexOf(chr) >= 0)

  showError(chr)

};

</script>


查看完整回答
反对 回复 2022-10-13
  • 2 回答
  • 0 关注
  • 109 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号