If checkForm is false, I don't want the form to submit. And if checkForm is true, I would like the form to submit and the action=POST to work. I've seen several examples online where people have VERY similar code and theirs supposedly works. So what am I doing wrong? Is there a really obvious mistake?
function checkForm(){
var checkinDate = $("#checkin").val();
var checkoutDate = $("#checkout").val();
var errorMsg = "";
var errorCount = 0;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
var todaysDate = mm+"/"+dd+"/"+yyyy;
if(checkinDate < todaysDate || checkoutDate < todaysDate || checkoutDate < checkinDate){
errorMsg += "There was an error with the check-in and/or check-out date you provided.\n\n";
errorCount++;
}
var adults = $("#adults").val();
if (adults == null){
errorMsg+="Please select the number of adults.\n\n";
errorCount++;
}
var children = $("#children").val();
if (children == null){
errorMsg +="Please select the number of children.\n\n";
errorCount++;
}
if(errorCount == 0){
return true;
}
return false;
}
And here is the form:
<form action="results.php" method="post" class="form-inline">
<span><strong>Check Availability:</strong></span>
<span class="bookingSpan">
<input type="text" id="checkin" class="form-control" value="Check-In Date">
</span>
<span class="bookingSpan">
<input type="text" id="checkout" class="form-control" placeholder="Check-Out Date">
</span>
<span class="bookingSpan">
<select class="selectpicker" id= "adults" name="adults" multiple title='Adults'>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</span>
<span class="bookingSpan">
<select class="selectpicker" id="children" name="children" multiple title='Children (Under 18)'>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</span>
<button type="submit" class="btn btn-default" onClick="return checkForm()">Submit</button>
</form>
The form validation works perfectly. If there are errors in any of the input, the alert box pops up, describing the errors. But no matter what (whether there are errors or not), the page goes to results.php.