Hello, I'm trying to learn how to validate a form and I have this problem:
When I run this code and I don't enter anything - the form should not send, but it does. My question is ... Why, and how can I fix it?
the html:
<form method="post" action="#">
<fieldset>
<legend>Formular</legend>
<label for="name">Name: </label>
<input type="text" class="required" name="name" value="" />
<br /><br />
<label for="pass">Password: </label>
<input type="password" class="required" name="pass" value="" />
<br /><br />
<input type="submit" value="Submit!" />
</fieldset>
</form>
the jQuery:
$('#obal form').submit(function() {
$('.required').each(function() {
if($(this).val() == "") {
if (errorCount === undefined) {
var errorCount = 1;
} else {
++errorCount;
}
}
});
if (errorCount > 0) {
window.alert( 'You didnt pass' );
return false;
} else {
return true;
}
});
If you know any other improvements the code could take, you can tell me aswell, thanks .. :-)