Hi!
Im trying to lern how to use JavaScript in my homepage.
Im trying to make an alert box come up if a person did not fill in the name of a form like this:
The HTML:
- Code:
-
<form method="POST" name="myform" action="http://formfixer.com/ff/ff.cgi" onSubmit="return validateForm();">
<input type="hidden" name="s_email" value="koanuka@capoeiras.se">
<input type="hidden" name="s_retur" value="http://www.koanuka.se/mah/registrering.html">
<p>Ditt namn:</p>
<p> <input type="text" size="20" name="namn"> </p>
<p>Din e-postadress:</p>
<p> <input type="text" size="20" name="email"> </p>
<input type="submit" value="Skicka">
</p>
</form>
The JavaScript:
- Code:
-
function validateForm() {
var name = document.myForm.username.value;
if (name == "") {
alert("Required field not filled in!");
return false;
} else return true;
}
My question is how do I "connect" the form to the JavaScript? I have a: <script type="text/javascript" src="myscript.js"> in the head of the html. But do I need to use "getElementById"?
Thankful for a pedagogical answer