Click to See Complete Forum and Search --> : Validating Checkbox Array


Arc
06-18-2003, 05:27 PM
I have an array of 3 checkboxes called "need". They each have different checked values. I am trying to see if none of the checkboxes were checked and if not then raise an alert.


I tried

if(document.form.need.value==""){
alert("SADD");
}

but that doesn't work.

If I pass the "need" value to my next page when none are checked it comes up empty... but when i check to see if it's empty with javascript it doesn't work.


Any ideas? Thanks!:D

Jona
06-18-2003, 05:38 PM
if(document.formName.need.checked != true){
alert("SADD");
return false;
}


Jona

Arc
06-18-2003, 06:35 PM
yah i tried that too

and

if(document.form.need.checked == false)

but it doesn't raise the alert. I dont get any errors or anything it just doesn't work.

heres some more code


function Validate(){
if(document.form.need.checked != True || document.form.name.value == "" || document.form.title.value == ""){
alert("Please make sure all required fields are filled in correctly");
}
}


It gives the alert if the 2 text boxes arent filled in but if i dont click any checkbox it does not raise an alert...

khaki
06-18-2003, 06:52 PM
hi arc...
(hi arc? high arc? hmmm.... never mind :rolleyes: )...

i'm no javascript expert... but i know how to copy and paste from my own working pages.
try this (hopefully i stripped it down and re-populated it for your needs properly)

;) k

function validate(frm)
{
var chk = false;
for (var counter=0; counter<frm.need.length; counter++)
if (frm.need[counter].checked == true)
chk = true;
if (chk == false)
{
alert('Please make sure all required fields are filled in correctly');
return false;
}
return true;
}

EDIT:
in case it isn't obvious enough:
onsubmit="return validate(this)"

Khalid Ali
06-18-2003, 06:58 PM
Originally posted by Arc
I have an array of 3 checkboxes called "need".
Any ideas? Thanks!:D

To go through arrays you will have to loop thrugh the array object index by index.

as you mentioned checkboxes array name is
need

there fore
you will need to do exactly as Khaki mentioned above..only one thing needs a bit of clarification.

function validate(frm)

the parameter frm in parenthesis points to the form name that has checkboxes as chilren.

In your code you have

document.form.need.checked == false

always try to avoid confusing names for elements.
document.form is not appropriate it might work on times, but it will cause problems for sure,this is because html form element name is form and you are assigning attribute name="form" as well.

Arc
06-18-2003, 10:23 PM
OK hmm i don;t get it... i am now getting erros saying object expected...

Here is the whole function


function Validate(){
if(document.form.need.checked != True || document.form.name.value == "" || document.form.title.value == ""){
var error = "!";
}//end if
var chk = false;
for (var counter=0; counter<document.form.need.length; counter++)
if (document.form.need[counter].checked == true)
chk=true;
}//end if
if(chk==false){
error="!";
}//end if
}//end for
if(error!=""){
alert('Please make sure all required fields are filled in correctly');
} //end if
}//end function

Jona
06-18-2003, 10:45 PM
What is your form named? Try naming it something else, to begin with. Also, when I said != true, notice that I did not capitalize "true." This isn't VB. ;)

If you could please post a link to your page, it would help.

Jona

khaki
06-18-2003, 10:49 PM
OK hmm i don;t get it ok ummm....
now I don't get it.

if you want to do it your way... do it your way.

i gave you working code :rolleyes:

fight it...
(or... ask)

but... just so ya know...
that's your function

not mine :rolleyes:...
;) k

Arc
06-18-2003, 11:13 PM
Originally posted by khaki
ok ummm....
now I don't get it.

if you want to do it your way... do it your way.

i gave you working code :rolleyes:

fight it...
(or... ask)

but... just so ya know...
that's your function

not mine :rolleyes:...
;) k

well all i did was add YOUR code to my already excisting WORKING code.

The part with the True with capital "T" was actually part of my old code that i didn't delete when i posted it here... it's actually not in the code i am now using..

All i did to YOUR code was add some comments so i can keep track of when an if statemnt ends... and i added an error var so i can keep track of wether to display an error... but the basic logic of YOUR code is till intact!;)

The name of my form is "form"... and no that's not the problem... i can rename it(and have) if it makes you feel better but, that's has nothing to do with the error i am getting.


function Validate(){
if(document.form.name.value == "" || document.form.title.value == ""){
var error = "!";
}//end if
var chk = false;
for (var counter=0; counter<document.form.need.length; counter++)
if (document.form.need[counter].checked == true)
chk=true;
}//end if
if(chk==false){
error="!";
}//end if
}//end for
if(error!=""){
alert('Please make sure all required fields are filled in correctly');
} //end if
}//end function


I appreciate the help but there's no reason to get a tude because i made a minor adjustment to the code you provided. I already had working code, i am just trying to figure out how to check for these checkboxes that's all.

Thanks!:D

Arc
06-18-2003, 11:16 PM
Thanks Dave, i think i will go with that first option. FOr what i am doing it's just alot easier to incorporate into my code :)

Jona
06-18-2003, 11:18 PM
Originally posted by Jona
If you could please post a link to your page, it would help.

You can probably solve your problem with what Dave said, but next time please read the replies thoroughly. If you are getting errors, there are many possible things causing it--and it is very possible that the code you have is fine but the error is caused elsewhere making the code not work.

Jona

khaki
06-18-2003, 11:23 PM
hey...
no 'tude here bro

you asked for help.
you decided to do it your own way.

that's cool.

i just pointed it out to you.
no need to 'tude me for it :rolleyes:

the code i gave you worked as-is.
you were the one who changed it and then questioned it.

what do you expect me to do...
drive to your house and type it too :)

the 'tude is yours...
the code remains my gift to you :rolleyes:

whatever
;) k