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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking checkboxes (the saga continues)

Status
Not open for further replies.

zippynz

Programmer
May 17, 2001
50
0
0
NZ
Hi,
I have been using the following script for a while now and have had 0 problems (thanks to the guy who gave it to me, can't remember who it was)

if(checkB(frmMain))
document.frmMain.submit()
else
alert('Please select at least one Bar')
return false;
}
function checkB(frm){
var flag=0
for (var ii=0; ii<frm.elements.length; ii++){
if (frm.elements[ii].name=='BarID'){
if (frm.elements[ii].checked) flag=-1
}
}

What it does is check the form to see whether any checkboxes have been checked, if they havent it returns an error.
Now i havent needed to use it in conjunction with other checks before and this is where I am having trouble. For some reason it brings up the error whether you check a box or not!

Obviously that isnt very useful, I was wondering if someone here could have a look at the code and find where the bug is? thanks a bunch in advanve :)

Here is the code:


<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function submitIt(frmMain) {
if (frmMain.NewStaffID.value == &quot;&quot;) {
alert(&quot;Please assign the Staff Member a Unique ID number&quot;)
frmMain.NewStaffID.focus()
return false
}
if (frmMain.Name.value == &quot;&quot;) {
alert(&quot;Please give them a name.&quot;)
frmMain.Name.focus()
return false
}
if (frmMain.Position.value == &quot;&quot;) {
alert(&quot;Please give them a Position.&quot;)
frmMain.Position.focus()
return false
}
if (frmMain.Mobile.value == &quot;&quot;) {
alert(&quot;What is their mobile number, please enter none for no mobile.&quot;)
frmMain.Mobile.focus()
return false
}
if (frmMain.Phone.value == &quot;&quot;) {
alert(&quot;What is their home phone number?&quot;)
frmMain.Phone.focus()
return false
}
if (frmMain.Rate.value == &quot;&quot;) {
alert(&quot;How much should they be paid?&quot;)
frmMain.Rate.focus()
return false
}
if(checkB(frmMain))
document.frmMain.submit()
else
alert('Please select at least one Bar')
return false;
}
function checkB(frm){
var flag=0
for (var ii=0; ii<frm.elements.length; ii++){
if (frm.elements[ii].name=='BarID'){
if (frm.elements[ii].checked) flag=-1
}
}
}
//-->
</SCRIPT>
<table border=&quot;1&quot; cellSpacing=&quot;0&quot; cellPadding=&quot;2&quot; bordercolordark=&quot;#000000&quot; bordercolorlight=&quot;#000000&quot;>
<form autocomplete=&quot;off&quot; action=/tools/staff.asp?action=update&mode=update method=&quot;post&quot; id=&quot;frmMain&quot; name=&quot;frmMain&quot; onSubmit=&quot;return submitIt(this);submitonce(this);&quot;>
<input type=&quot;hidden&quot; name=&quot;StaffID&quot; value=&quot;5612&quot;>
<tr>
<td background=&quot;/images/rows/dark.gif&quot;>Staff ID:</td>
<td background=&quot;/images/rows/dark.gif&quot;><input type=&quot;text&quot; name=&quot;NewStaffID&quot; value=&quot;5612&quot; size=&quot;5&quot;></td>
</tr>
<tr>
<td background=&quot;/images/rows/light.gif&quot;>Bar's:</td>
<td background=&quot;/images/rows/light.gif&quot;>
<table cellSpacing=&quot;0&quot; cellPadding=&quot;0&quot;>
<tr>

<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;7&quot;></td><td>Calneva</td>

<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;14&quot;></td><td>Developer</td>

<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;15&quot; checked=&quot;checked&quot;></td><td>Exchange</td>
</tr><tr>
<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;1&quot;></td><td>Komodo</td>

<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;2&quot;></td><td>Margaritas Auckland</td>

<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;13&quot;></td><td>Margaritas Ohakune</td>
</tr><tr>
<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;11&quot;></td><td>Nancys</td>

<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;5&quot;></td><td>Provedor</td>

<td><input type=&quot;checkbox&quot; id=&quot;BarID&quot; name=&quot;BarID&quot; value=&quot;12&quot;></td><td>Waterfront Cafe</td>
</tr><tr>
</tr>
</table>
</td>
</tr>

<tr>
<td background=&quot;/images/rows/dark.gif&quot;>Name:</td>
<td background=&quot;/images/rows/dark.gif&quot;><input type=&quot;text&quot; name=&quot;Name&quot; value=&quot;JOEY&quot;><input type=&quot;hidden&quot; name=&quot;arrStaffID&quot; value=&quot;5612&quot;></td>
</tr>
<tr>
<td background=&quot;/images/rows/light.gif&quot;>Position:</td>
<td background=&quot;/images/rows/light.gif&quot;><input type=&quot;text&quot; name=&quot;Position&quot; value=&quot;cleaner&quot;> (what do they do?)</td>
</tr>
<tr>
<td background=&quot;/images/rows/dark.gif&quot;>Mobile Number:</td>
<td background=&quot;/images/rows/dark.gif&quot;><input type=&quot;text&quot; name=&quot;Mobile&quot; value=&quot;1234&quot;> (if none type none)</td>
</tr>
<tr>
<td background=&quot;/images/rows/light.gif&quot;>Phone Number:</td>
<td background=&quot;/images/rows/light.gif&quot;><input type=&quot;text&quot; name=&quot;Phone&quot; value=&quot;567&quot;></td>
</tr>
<tr>
<td background=&quot;/images/rows/dark.gif&quot;>Pay Rate:</td>
<td background=&quot;/images/rows/dark.gif&quot;>$<input type=&quot;text&quot; name=&quot;Rate&quot; value=&quot;0.5&quot; size=&quot;5&quot;></td>
</tr>
<tr>
<td colspan=&quot;2&quot; align=&quot;center&quot;><input type=&quot;submit&quot;></td>
</tr>
</form>
</table>
 
Your CheckB function isn't returning a value, therefore the if statement:

if(checkB(frmMain))

will never be true and the false condition will always execute. Try something like this:
Code:
    function checkB(frm){
    var flag=0
    for (var ii=0; ii<frm.elements.length; ii++){
        if (frm.elements[ii].name=='BarID'){
            if (frm.elements[ii].checked) 
                flag = 1;
        }
    if(flag)
        return true;
    else
        return false;
    }
 
hie
looks like u've lost smth:
function checkB(frm){
var flag=0
for (var ii=0; ii<frm.elements.length; ii++){
if (frm.elements[ii].name=='BarID'){
if (frm.elements[ii].checked) flag=-1
}
}
if (flag) return true
else return false

}
this shuld fix it.. Victor
 
darn! i'm too sleepy!!
sorry for doubling ur post, bvallet; when i refreshed my ie it wasnt there! i swear! Victor
 
and once again it works like a charm!
thanks guys :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top