我想要一個文本框只接受字母字符,我相信這是用每個字符輸入後調用的當前函數完成的。但是,我也想在用戶嘗試輸入非字母字符的字符時返回錯誤消息。任何人都可以解釋這個問題的可能解決方案?無效字符輸入上的JavaScript返回錯誤消息
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
/* <![CDATA[ */
function validateLetter() {
var textInput = document.getElementById("maiden").value;
textInput = textInput.replace(/[^A-Za-z]/g, "");
document.getElementById("maiden").value = textInput;
}
/* ]] */
</script>
</head>
<body>
<form action="validateThePassword" enctype="application/x-www-form-urlencoded">
<p>Enter mother's maiden name:
<input type="text" id="maiden" name="maiden" onkeypress="validateLetter()" </p>
</form>
</body>
</html>
務必要關閉你的'input'標籤。 – nothingisnecessary