I am a beginner and I'm trying to write a simple form validation code. My HTML is the following:
<h1 align="center">Super Accurate Cool or Not Test</h1>
<div id="box" align="center">
<P>Find out if you are cool or not today!!!</p>
<form action="#" name="form1" onsubmit="return coolTest()" method="POST">
<label>Enter your name:</label>
<input type="text" id="inputName" name="inputName">
<br>
<input type="submit" value="Submit" id="submit">
</form>
</div>
My JavaScript is this:
var name = document.form1.getElementById("inputName").value;
function coolTest() {
if (name === "Joe") {
alert("You are so cool!");
} else if (name === "Bob") {
alert("You are kinda cool, but not as cool as Joe.");
} else if (name === "") {
alert("Please enter your name");
} else {
alert("You are not cool... Sorry.");
}
}
What am I doing wrong here that is creating this to not work? Thank you! Also- here is a jsfiddle I already have: http://jsfiddle.net/tjhillard/ZQTq5/4/embedded/result/

onkeyup="coolTest();"in the text input instead. That way they'll get an alert as soon as they type the e in Joe without having to submit.