Quote:
<script>
var keeperrank = document.getElementById("keeperrank");
if (keeperrank.value == '' || keeperrank.value != "Senior" || keeperrank.value != "Junior" || keeperrank.value != "Standard"){
messages.push("Please Enter Valid Ranks Only")
return false;
}
else return true;
</script>
Your script runs
once, when the page first loads. At that point, the user hasn't had a chance to type anything in yet. Your
return
statements don't do anything, since there's no function to return from. And you'll get a script error, since you haven't defined the
messages
variable anywhere.
You also haven't added an
ID
to your
keeperrank
field, so
document.getElementById("keeperrank")
will return
null
, giving you more script errors.
You need to add the
ID
attribute to the
keeperrank
element, and run your script in response to the form being submitted:
document.addEventListener("submit", function(e) {
if (e.target.tagName.toUpperCase() !== "FORM") { return; }
const rank = document.getElementById("keeperrank").value;
if (rank === "") {
alert("Please enter a rank");
e.preventDefault();
return;
}
if (rank !== "Senior" && rank !== "Junior" && rank !== "Standard") {
alert("Please enter a valid rank");
e.preventDefault();
return;
}
});
If you want to force the correct value, you could use a
<select>
instead of an
<input>
, avoiding the need to use script at all:
<p>Keeper rank: <select name="keeperrank" required>
<option>(Select one)</option>
<option value="Senior">Senior</option>
<option value="Junior">Junior</option>
<option value="Standard">Standard</option>
</select></p>
<select>: The HTML Select element - HTML: HyperText Markup Language | MDN[
^]