TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
regor
NA
61
0
Validating CheckBoxList javascript & C# help needed
Feb 18 2010 10:00 AM
Hi,
I have the following form
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
<igmisc:WebGroupBox ID="WebGroupBox1" runat="server" Text="Toxic">
<Template>
<asp:CheckBox ID="CheckBoxToxic" runat="server" Text="Toxic" /><br />
<table>
<tr>
<td style="width: 100px">
Solid</td>
<td style="width: 100px">
Liquid</td>
</tr>
<tr>
<td style="width: 100px">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="1" Text="1" ></asp:ListItem>
<asp:ListItem Value="2" Text="2" ></asp:ListItem>
<asp:ListItem Value="3" Text="3" ></asp:ListItem>
<asp:ListItem Value="4" Text="4" ></asp:ListItem>
<asp:ListItem Value="5" Text="5" ></asp:ListItem>
</asp:CheckBoxList></td>
<td style="width: 100px">
<asp:CheckBoxList ID="CheckBoxList2" runat="server">
<asp:ListItem Value="1" Text="1" ></asp:ListItem>
<asp:ListItem Value="2" Text="2" ></asp:ListItem>
<asp:ListItem Value="3" Text="3" ></asp:ListItem>
<asp:ListItem Value="4" Text="4" ></asp:ListItem>
<asp:ListItem Value="5" Text="5" ></asp:ListItem>
</asp:CheckBoxList></td>
</tr>
</table>
</Template>
</igmisc:WebGroupBox>
<br />
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Approve</asp:ListItem>
<asp:ListItem>Reject</asp:ListItem>
</asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return validate()" /><br />
<br />
</div>
</form>
And I want to create a function in which if first checks the input in the textbox, then that the checkbox is checked. I have got as far as that but now I need a validation to check that the user has selected at least 1 of the checkboxes in the CheckBoxList.
This is the javascript function I have so far:
if(document.getElementById("<%=TextBox1.ClientID%>").value == "")
{
alert("Select a Requestor from the New Product Grid");
document.getElementById("<%=TextBox1.ClientID%>").focus();
return false;
}
else
{
var oDDL = document.all("DropDownList1");
var curText = oDDL.options[oDDL.selectedIndex].text;
if(curText != "Reject")
{
var chk = document.getElementById("<%=CheckBoxToxic.ClientID%>");
if(chk.checked)
{
}
}
}
I have found a way to do this in C# but I dont want to work under the server, would rather do this in the clientside.
C# validation:
string errorResults = "";
if (appProdRequestorTextBox.Text == "")
{
errorResults = "Select a Requestor from the New Product Grid";
ErrorSummary.AddError(errorResults, this);
}
else
{
if (appProdApprovalComboBox.Text != "Reject")
{
//Validate Fields
if (appProdToxicCheckBox.Checked == true)
{
if (func.ItemCountGreaterZero(appProdToxicSolidCheckedListBox) == false && func.ItemCountGreaterZero(appProdToxicLiquidCheckedListBox) == false)
{
errorResults = "Select at least one Toxic Storage Location";
ErrorSummary.AddError(errorResults, this);
}
}
Where the ItemCountGreaterZero is:
public bool ItemCountGreaterZero(CheckBoxList chkLstBx)
{
int selectedCount = 0;
for (int i = 0; i < chkLstBx.Items.Count; i++)
{
if (chkLstBx.Items[i].Selected)
{
selectedCount++;
}
}
if (selectedCount > 0)
{
chkLstBx.Focus();
return true;
}
else
{
chkLstBx.Focus();
return false;
}
}
Again, I would preffer using javascript but I am not familiar in how to use different functions and how to call validations for a checkboxlist, neither do I seem to be able to find one.
Reply
Answers (
3
)
track the project
placing accordion control inside tab control