私のコードを正しくチェックしていないjavascriptに問題があります。この場合、具体的に何が問題なのか誰かが教えてくれることを願っています。私が達成しようとしているのは、フィールドのいずれかが空白の場合、またはパスワード フィールドが 8 文字未満の場合、可能な結果のいずれかでアラートが表示されることです。ただし、すべてのフィールドを空白以外に入力すると、フォームが送信されて次のページに移動します。ここで何か基本的なことが欠けていますか?
<script>
function validateForm()
{
//var x=document.register.username.value
var x="document.forms["register"]["username"].value";
var y="document.forms["register"]["homeaddress"].value";
var z="document.forms["register"]["password"].value";
if (x == "" || y == "" || z == "") {
alert("You must fill in all fields in the application");
return false;
} else if (z.length < 8) {
alert("Passwords must be at least 8 characters long");
return false;
}
return true;
}
</script>
フォームは次のとおりです。
<form name="register" method="post" action="tryregister.php" onsubmit="return validateForm();">
Username: <input type="text" name="username" maxlength="50"><br>
Home address: <input type="text" name="homeaddress" maxlength="50"><BR>
Password: <input type="password" name="password" maxlength="50"><br>
<input type="submit" value="Register me!">