Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking if a checkbox has a value 1

Status
Not open for further replies.

zippynz

Programmer
May 17, 2001
50
NZ
Hi I am trying to check to see if a check box has a value,
the checkboxes are built from an asp script, there can be as many as 1 to 10 boxes and they all have the same name,
when i submit the form i want to check to see whether at least 1 of the boxes have been checked, if it hasnt i want to return a message box

this is the script im using:
Code:
function checkTheBoxes()
	if (frmSelectPictures.selectimageid.value == "") {
		alert('Please check a box.')
		return false;
	}
 
hi
use this function:

function checkB(frm,chBname){
var flag=0
for (var ii=0; ii<frm.elements.length; ii++){
if (frm.elements[ii].name==chBname){
if (frm.elements[ii].checked) flag=-1
}
}
return flag
}

it returns -1 (true) if any of checkboxes called chBname selected

your form:

<form name=nnn>
<input type=checkbox name='shh'>a
<input type=checkbox name='shh'>b
<input type=checkbox name='shh'>c
<input type=checkbox name='shh'>d<br>
<input type=button value=&quot;see if any of checkboxes selected&quot; onclick='javascript:alert(checkB(document.forms.nnn,&quot;shh&quot;))'><br>
</form>


regards, vic
 
hmm i couldnt get this to work?
i copied the exact script you wrote and it just brings up some errors,
any ideas why?

thanks for your help
 
hi

have no idea... here is the complete code i use, it works both in ie & nn (i checked ie5.0 & nn4.6) what brawser do u use?

<html>
<head>
<title>checkboxes</title>
<script>
function checkB(frm,chBname){
var flag=0
for (var ii=0; ii<frm.elements.length; ii++){
if (frm.elements[ii].name==chBname){
if (frm.elements[ii].checked) flag=-1
}
}
return flag
}
</script>
</head>
<body bgcolor=&quot;#FFFFFF&quot; >
<form name=nnn>
<input type=checkbox name='shh'>a
<input type=checkbox name='shh'>b
<input type=checkbox name='shh'>c
<input type=checkbox name='shh'>d<br>
<input type=button value=&quot;see if any of checkboxes selected&quot; onclick='javascript:alert(checkB(document.forms.nnn,&quot;shh&quot;))'><br>
</form>
</body>
</html>


just copy-paste it

regards
 
well that seems to have worked...
i will try modifying it a bit now and see if i can get it to work with my other code,

im using IE 5
 
except...
instead of returning -1 i want it to just keep processing as normal,
how can i make it do that?
sorry im a bit of a javascript begineer,
thanks for your help
 
>>instead of returning -1 i want it to just keep processing as normal,
how can i make it do that?

explain a bit more what do u mean... (sorry, english is not my native & sometimes i just cant get what pple want from me)

regards
 
well all i want to do is for that script to check to see if none of the boxes have been checked,
if they have checked a box that is ok and the form can be submitted,
so instead of an alert box opening i just want to let the form submit
 
ow got it - its simple:
must be smth like this (if i'm not right someone wuld correct me though)
the hole code is prev., but:

function submitin(frm,chBname){
if(checkB(frm,chBname)) frm.submit()
else
//your actions: either alert box or html file displayin 2 check those boxes
alert('plz check cmth')
}


& your input tag in form:

<input type=button value=&quot;submit&quot; onclick='javascript: submitin(document.forms.nnn,&quot;shh&quot;)'>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top