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>

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>
添加回答
舉報