By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,713 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,713 IT Pros & Developers. It's quick & easy.

checkbox check

P: n/a
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!
Sep 30 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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()' ...

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 30 '05 #2

P: n/a
alu

"Evertjan." wrote
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()' ...

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

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");
}
}
------------------------
given a form named 'form1' and checkboxes named 'ch1' etc.

-alu
Sep 30 '05 #3

P: n/a
alu wrote on 01 okt 2005 in comp.lang.javascript:
"Evertjan." wrote
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()' ...

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;
}
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Sep 30 '05 #4

P: n/a

"John" <gt*****@mail.gatech.edu> wrote in message
news:dh**********@news-int2.gatech.edu...
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.


dont use a check box use a radio group....

Sep 30 '05 #5

P: n/a
alu

"Zoe Brown" <zo***********@N-O-S-P-A-A-Mtesco.net> wrote in message
news:iR****************@newsfe5-gui.ntli.net...

"John" <gt*****@mail.gatech.edu> wrote in message
news:dh**********@news-int2.gatech.edu...
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.


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
Sep 30 '05 #6

P: n/a
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");
}
}
------------------------
given a form named 'form1'
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[i].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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 1 '05 #7

P: n/a

"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn*******************@194.109.133.242...
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!
Oct 3 '05 #8

P: n/a
John wrote on 03 okt 2005 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in message
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.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 3 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.