В поле ввода нельзя вводить другие символы - PullRequest
0 голосов
/ 21 июня 2020

Могу ли я узнать, из-за какой ошибки в моем поле ввода нельзя вводить символы? Поскольку после установки ></\":*?| эти символы ограничиваются в поле ввода, другой символ не может быть введен в поле ввода.

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

0 голосов
/ 21 июня 2020

Просто удалите 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>
0 голосов
/ 21 июня 2020
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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...