首页 > 解决方案 > 输入字段不能输入其他字符

问题描述

我可以知道是什么错误导致我的输入字段无法输入字符吗?因为在输入字段中设置></\":*?|了这些符号限制后,其他字符无法在输入字段中输入。

<!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>

希望有人能帮我解决这个问题。谢谢。

标签: javascripthtml

解决方案


只需删除 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>


推荐阅读