checkbox check

J

John

Hey guys,

In a form of 3 checkboxes, at least one of them has to be checked. How would
I go about making sure at least one is checked before the form is submitted?
If it's not checked, an alert pops up saying at least one has to be checked.

Thanks in advance!
 
E

Evertjan.

John wrote on 30 sep 2005 in comp.lang.javascript:
In a form of 3 checkboxes, at least one of them has to be checked. How
would I go about making sure at least one is checked before the form
is submitted? If it's not checked, an alert pops up saying at least
one has to be checked.

<form onsubmit='return checkForIt()' ...
 
A

alu

John wrote on 30 sep 2005 in comp.lang.javascript:


<form onsubmit='return checkForIt()' ...


if it helps, checkForIt() would look something like
------------------------
function checkForIt(){
fr1 = document.forms["form1"];
if(!fr1.ch1.checked && !fr1.ch2.checked && !fr1.ch3.checked){
alert("at least one needs to be checked");
}
}
 
E

Evertjan.

alu wrote on 01 okt 2005 in comp.lang.javascript:
John wrote on 30 sep 2005 in comp.lang.javascript:


<form onsubmit='return checkForIt()' ...


if it helps, checkForIt() would look something like
------------------------
function checkForIt(){
fr1 = document.forms["form1"];
if(!fr1.ch1.checked && !fr1.ch2.checked && !fr1.ch3.checked){
alert("at least one needs to be checked");
}
}

The function needs to return true/false:

<form onsubmit='return checkForIt()' ...

....

function checkForIt(){
fr1 = document.forms["form1"];
if (fr1.ch1.checked || fr1.ch2.checked || fr1.ch3.checked)
return true;
alert("At least one checkbox needs to be checked");
return false;
}
 
A

alu

Zoe Brown said:
dont use a check box use a radio group....


radio group for ONLY one checked
check boxes for AT LEAST one checked, which is the requirement.
-alu
 
M

Michael Winter

On 30/09/2005 23:00, alu wrote:

[snip]
function checkForIt(){
fr1 = document.forms["form1"];
if(!fr1.ch1.checked && !fr1.ch2.checked && !fr1.ch3.checked){
alert("at least one needs to be checked");
}
}

It would be better, in my opinion, to pass a reference to the form when
calling the function (making a form name/id redundant).
and checkboxes named 'ch1' etc.

If the checkboxes are related, they should have the same name attribute
value.

function isChecked(group) {
for(var i = 0, n = group.length; i < n; ++i) {
if(group.checked) {return true;}
}
return false;
}
function validate(form) {
var elements = form.elements;

if(!isChecked(elements['cbox-name'])) {
alert('At least one checkbox should be selected.');
return false;
}
return true;
}


<form action="..." onsubmit="return validate(this);">

Or some variation, thereof.

Mike
 
J

John

Evertjan. said:
function checkForIt(){
fr1 = document.forms["form1"];
if (fr1.ch1.checked || fr1.ch2.checked || fr1.ch3.checked)
return true;
alert("At least one checkbox needs to be checked");
return false;
}

Thanks! How would I go about adding syntax to make it check for another
function to be true before it returns true as a whole?

For instance:

if (fr1.ch1.checked || fr1.ch2.checked || fr1.ch3.checked) && (function
blabla return true)
return true;

Thanks in advance!
 
E

Evertjan.

John wrote on 03 okt 2005 in comp.lang.javascript:
Evertjan. said:
function checkForIt(){
fr1 = document.forms["form1"];
if (fr1.ch1.checked || fr1.ch2.checked || fr1.ch3.checked)
return true;
alert("At least one checkbox needs to be checked");
return false;
}

Thanks! How would I go about adding syntax to make it check for another
function to be true before it returns true as a whole?

For instance:

if (fr1.ch1.checked || fr1.ch2.checked || fr1.ch3.checked) && (function
blabla return true)
return true;

Try it out for yourself. The above won't work, John.
Letting others do all the work is not the way of this NG.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top