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

Checked at least 1 checkbox on submit

Status
Not open for further replies.

joelxez

Programmer
Apr 18, 2002
67
PH
hi experts,

i have script, it works only if there are more than 1 checkbox. But it doesnt work if there is only one checkbox present.

Any one who can figure out?


function validateForm()
{
obj = document.formname.elements("chkbox");
for (i = 0; i < obj.length; i++)
{
if (obj.checked)
{
alert("Good Job")
}

}
alert("Select Item from the list")
return false;
}

 
function validateForm()
{
obj = document.formname.elements("chkbox");
if(typeof(obj.length)!="undefined")
{
for (i = 0; i < obj.length; i++)
{
if (obj.checked)
{
alert("Good Job")
}

}
alert("Select Item from the list")
return false;
}
else
{
if (obj.checked)
{
alert("Good Job")
}

}
alert("Select Item from the list")
return false;
}
}


note the typeof() function...


Known is handfull, Unknown is worldfull
 
hi vbkris

it seems repeated alert messages is working..

jowel,
 
sorry my mistake:

function validateForm()
{
obj = document.formname.elements("chkbox");
if(typeof(obj.length)!="undefined")
{
for (i = 0; i < obj.length; i++)
{
if (obj.checked)
{
alert("Good Job")
}

}
alert("Select Item from the list")
return false;
}
else
{
if (!obj.checked)
{
alert("Select Item from the list")
return false;
}

}
}
}


i havent touched the multiple checkbox code part at all, only single checkbox code...

Known is handfull, Unknown is worldfull
 
vbkris-

is there any reason you're using elements() instead of elements[]?

joel-

is this what you're looking for?

Code:
function validateForm() {
    var isOneChecked = false;
    var cb = document.forms['f'].elements['chkbox'];
    for ( var i = 0; i < cb.length; i++ ) {
        if (cb[i].checked) isOneChecked = true;
    }  

    if (isOneChecked) {
        alert( "valid" );
    } else {
        alert( "not valid" );
        return false;
    }
}

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
my mistake, when joe said the multiple checkbox situation are working i assumed his code is correct...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top