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

dynamic checkbox validation 1

Status
Not open for further replies.

allyson212

Programmer
Mar 11, 2004
7
US
I have a form that I am validating that contains a dynamically generated list of checkboxes. I know how to validate them individually as I have done in the code below, if there is a set number of them. However currently this generated list has 45 values and although I could just repeat the code 45 times there is the possibility that the number of items could change. Is there a way to loop through the items and check to see if at least on value is selected out of the list values for a particular name?

Here is what I have so far. Any help would be very much appreciated.

Thanks,
Allyson

<script language="JavaScript">
<!--
function Validate()
{
if (document.formcheck.Level_of_Degree.selectedIndex == 0)
{
alert("Please select a Degree Level.");
return false;
}

else if (document.formcheck.UndergradHours.value=="")
{
alert("Please enter a value for Hours.");
return false;
}

else if (( document.formcheck.UndergradClasses[0].checked == false )
&& ( document.formcheck.UndergradClasses[1].checked == false )
&& ( document.formcheck.UndergradClasses[2].checked == false )
&& ( document.formcheck.UndergradClasses[3].checked == false )
&& ( document.formcheck.UndergradClasses[4].checked == false )
&& ( document.formcheck.UndergradClasses[5].checked == false )
)
{
alert("Please choose at least one grade level.");
return false;
}

return true;
}
//-->
</script>


<form method="post" action="fmpro" name="formcheck">

<select name="Level_of_Degree">
<option>Bachelors</option>
<option>Masters</option>
<option>Specialist</option>
<option>Doctorate</option>
</select>

<input type="text" name="UndergradHours" value="">
 
try looping thru your checkboxes like this:
Code:
function Validate()
{
    if (document.formcheck.Level_of_Degree.selectedIndex == 0)
    {
    alert("Please select a Degree Level.");
    return false;
    }

    else if (document.formcheck.UndergradHours.value=="")
    {
    alert("Please enter a value for Hours.");
    return false;
    }

    else {
        var ugFlag = true;
        for (i = 0; i < formcheck.UndergradClasses.length; i++) {
            if (!formcheck.UndergradClasses[i].checked) {
               ugFlag = false;
            }
        }
        if (!ugFlag) {
            alert("Please choose at least one grade level.");
            return false;
        }
    }

    return true;
}

-kaht

banghead.gif
 
Thank you for your reply. I tried the code, but it will not allow the form to submit even if a value is selected. I tried also selecting a few checkboxes, but it still flashes the error.

Any ideas what would be stopping it?
 
sorry, I did that part backwards, here's the fixed portion of code:
Code:
    else {
        var ugFlag = false;
        for (i = 0; i < formcheck.UndergradClasses.length; i++) {
            if (formcheck.UndergradClasses[i].checked) {
               ugFlag = true;
            }
        }
        if (!ugFlag) {
            alert("Please choose at least one grade level.");
            return false;
        }
    }

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top