Jump to content

check form for input in two checboxes if both blank, dont submit


jesushax

Recommended Posts

Hi all, as the title heopfully explains

 

i have to checkboxes on my form

 

if neither of them are checked id like to display an error message and dont submit until one of them has a tick in it

 

can anyone help me out?

 

Cheers

Yup, give your checkboxes different ID's.

 

<input type="checkbox" name="checkbox1" id="checkbox1" value="check1_value" /><br />
<input type="checkbox" name="checkbox2" id="checkbox2" value="check2_value" />

 

Then make a JavaScript function.

 

<script language="javascript">
function checkboxes(){
var check1 = document.getElementById("checkbox1");
var check2 = document.getElementById("checkbox2");

if(check1.checked || check2.checked){
	return true;
}else{
	alert("Please check a box!");
	return false;
}
}
</script>

 

Then in you're submit button you need to call the function but with a return beforehand.

 

<input type="submit" name="submit" id="submit" onclick="return checkboxes();" />

 

Good luck.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.