I am trying to validate a form with multiple fields. Using the current js I can successfully validate every field/input except the checkboxes. I want to make sure that user submitting the form checks at least one checkbox (suites[]). For reading convenience I have deleted the other fields thus you see checkboxes only.
Below you will find τhe html form and the js:
< script type = "text/javascript" >
function validateForm() {
var name = document.forms["booking"]["name"].value;
var surname = document.forms["booking"]["surname"].value;
var city = document.forms["booking"]["city"].value;
var country = document.forms["booking"]["country"].value;
var phone = document.forms["booking"]["phone"].value;
var email = document.forms["booking"]["email"].value;
var arrival = document.forms["booking"]["arrival"].value;
var departure = document.forms["booking"]["departure"].value;
var suites = document.forms["booking"]["suites"].value;
var persons = document.forms["booking"]["persons"].value;
var payment = document.forms["booking"]["payment"].value;
if (name === null || name === "" || surname === null || surname === "" || city === null || city === "" || country === null || country === "" || phone === null || phone === "" || email === null || email === "" || arrival === null || arrival === "" || departure === null || departure === "" || persons === null || persons === "" || payment === null || payment === "" || suites === null || suites === "") {
alert("PLEASE FILL ALL REQUIRED FIELDS(*)");
return false;
}
} < /script>
<form name="booking" method="post" onsubmit="return validateForm()" action="procbook.php">
<div class="row">
<div class="col-sm-6 col-xs-12">
<div class="form-group">
<label>Suites<span class="required">*</span>
</label>
<div class="checkbox">
<label>
<input name="suites[]" type="checkbox" value="suite1">suite1
</label>
<label>
<input name="suites[]" type="checkbox" value="suite2">suite2
</label>
<label>
<input name="suites[]" type="checkbox" value="suite3">suite3
</label>
<label>
<input name="suites[]" type="checkbox" value="suite4">suite4
</label>
<label>
<input name="suites[]" type="checkbox" value="suite5">suite5
</label>
</div>
</div>
</div>
</div>
</form>
I would appreciate any suggestion, advise, help.
Thanks.
Aucun commentaire:
Enregistrer un commentaire