Evan Sap Evan Sap - 6 months ago 7
HTML Question

Something wrong in my checkbox validation?

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;
}
var elements = document.getElementsByName('suites[]');
var flag = false;
for(i = 0; i < elements.length; i++) {
if(elements[i].checked == true) {
//flag = true;
return true;
}else{
alert("PLEASE SELECT AT LEAST ONE SUITE(*)");
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.

Answer

What I would do is loop over the checkbox inputs and see if there is at least one checked. If so, I would set a flag to true. Let me show you an example:

function validateForm() {
    var elements = document.getElementsByName('suites[]');
    var flag = false;
    for(i = 0; i < elements.length; i++) {
        if(elements[i].checked == true) {
            flag = true;
        }
    }
}

You can then use the flag to check whether or not at least one of them was true.