473,654 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1878
John wrote on 30 sep 2005 in comp.lang.javas cript:
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='retur n 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.javas cript:
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='retur n 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.che cked && !fr1.ch2.checke d && !fr1.ch3.checke d){
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.javas cript:
"Evertjan." wrote
John wrote on 30 sep 2005 in comp.lang.javas cript:
> 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='retur n checkForIt()' ...

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


The function needs to return true/false:

<form onsubmit='retur n checkForIt()' ...

....

function checkForIt(){
fr1 = document.forms["form1"];
if (fr1.ch1.checke d || 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.g atech.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******** ********@newsfe 5-gui.ntli.net...

"John" <gt*****@mail.g atech.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.che cked && !fr1.ch2.checke d && !fr1.ch3.checke d){
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(e lements['cbox-name'])) {
alert('At least one checkbox should be selected.');
return false;
}
return true;
}
<form action="..." onsubmit="retur n 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.checke d || 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.checke d || 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.javas cript:
"Evertjan." <ex************ **@interxnl.net > wrote in message
function checkForIt(){
fr1 = document.forms["form1"];
if (fr1.ch1.checke d || 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.checke d || 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
1897
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)" ..... <input type="checkbox" name=ck id=ck onclick="check(this.form)" </form>
4
4624
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 type="checkbox" name="chk_Complete" value="<%Response.Write l_IsChecked%>"<%if cbool(l_IsChecked) then Response.Write " checked"%>> The code to captures the checkbox value in the asp page that builds the sql string is follows
4
26965
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" id="id_number" name="check" value="2" onclick="show()"/> <input type="checkbox" id="id_number" name="check" value="3"
4
3388
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 (i=0;i<form.elements.length;i++){ THATname = form.elements.name.split("-"); if (Sname.length==1){ if (THATname==Sname){
1
4151
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 dgRegGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgRegGrid.PageIndexChanged I then do a loop to check the checkbox state.
4
5944
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" id="cbItem" runat="server" onclick="test() value='<%#DataBinder.Eval(Container.DataItem, "PartNo")%>' NAME="cbItem"><%#DataBinder.Eval(Container.DataItem, "PartNo")%><td class='item_content' width='83'><input value=4 maxLength='4' size='4'...
1
1109
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 checkbox i need to check all others. But i don't can to use the ItemDataBoundEvent, because this event, is called once only. I need to check all checkbox of column when the user to check this checkbox of header section.
6
4341
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 I should post it. I have created a form, which consists of a list of items, each with a checkbox. When a checkbox is checked or unchecked, the page should be refreshed. During the refresh, the data is validated and the MySQL database is...
29
3137
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. Does each checkbox name have to be unique? I was hoping to just group them under one name, and select from that array. PHP is not my native language - I'm coming at this from Perl, so bear with me, things are a little different in that country.
10
5195
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. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named 'CheckAllRows' on the CheckedChanged event of this CheckBox. The CheckBox in the Header exists within the HeaderTemplate of a TemplateColumn in the...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.