I have a form and I trying to validate fields which have same class. required
<form id="HotelBooking" method="post" action="#">
<input type="text" class="required" name="vardas" />
<input type="text" class="required" name="pav" />
<input type="text" class="required" name="tel" />
<input type="submit" name="sb" value="Tvirtinti" onclick="tikrinti();"/>
</form>
JS code works for firs field, it shows error, but form still submits and resets the fields.
$(document).ready(function() {
$('#HotelBooking').submit(function () {
$(".required").each(function(){
if($(this).val().length == 0)
{
alert('Please Enter your value');
$(this).focus();
return false;
}
});
});
});
What I’m doing wrong?
You should put the
return falseoutside ofeach():