473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

checkbox check

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
8 1869
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
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
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

"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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fred | last post by:
Hi, I defined a form consisting of checkboxes like: <form> <input type="checkbox" name=ck id=ck onclick="check(this.form)" <input type="checkbox" name=ck id=ck onclick="check(this.form)" ........
4
by: Jack | last post by:
Hi, I have a checkbox the value which goes to a database via a asp page that builds the sql string. In the front end asp page, the checkbox code is written as follows: <i><input...
4
by: Piotr | last post by:
how can I read (in alert for example) array index number of checked checkbox? I have: <input type="checkbox" id="id_number" name="check" value="1" onclick="show()"/> <input type="checkbox"...
4
by: feanor | last post by:
I need to select children checkboxes when selecting the parent one. This is my function: function SelectChildrens(checkbox_name){ form = document.forms; Sname = checkbox_name.split("-"); for...
1
by: iforsyth | last post by:
Have a paging datagrid with a checkbox control in column(0). ViewState is enabled. I check the checkbox in first row of the grid on a page and then the program hits this event: Private Sub...
4
by: mahsa | last post by:
my problem dosent solve yet I want to have claa void in onclick of my check bov if I us function setProps ( ) msg.innerText = "llll"; in <datalist> <ItemTemplate><input type="checkbox"...
1
by: Dexter | last post by:
Hello all, I have a datagrid with a template column with checkbox, and at this template column, at header section, i have a checkbox with autopostback property as true, and when i check this...
6
by: Daz | last post by:
Hi everyone. Firstly, I apologise if this i not what you would call a PHP problem. I get quite confused as to what lives in which realm, so if this shouldn't be posted here, please suggest where...
29
by: Amer Neely | last post by:
I've got a dynamically built form with checkboxes for each element ( a list of file names in a directory). I need to grab only those checkboxes that are checked, so I can then delete those files. ...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.