I usually work with PHP so sadly don\'t have some basic JS principles down. This is all I want to accomplish--I\'ve seen many posts on this topic but they are usually beyon
Another Simple way is to create & invoke the function validate()
when the form loads & when submit button is clicked.
By using checked
property we check whether the checkbox is selected or not.
cbox[0]
has an index 0
which is used to access the first value (i.e Male) with name="gender"
You can do the following:
function validate() {
var cbox = document.forms["myForm"]["gender"];
if (
cbox[0].checked == false &&
cbox[1].checked == false &&
cbox[2].checked == false
) {
alert("Please Select Gender");
return false;
} else {
alert("Successfully Submited");
return true;
}
}
Demo: CodePen