hi all
i have one form in which i have multiple checkbox options for the user to select each checkbox. i want to validate each chekbox when click on the Button control. if any checkbox is not checked it will show error for a particular checkbox that is not checked.
Loading

Rohatash KumarPosted Apr 28, 2011, 5:23 AM
Ankit NandekarPosted Apr 28, 2011, 3:36 AM
omkar mhaiskarPosted Apr 28, 2011, 2:30 AM
you can check it through Custom validator and use validation summary to show ewrror message.
I think this will help:
function ValidateAnswerRadio()
{
var list = document.getElementById('<%=RadioButtons.ClientID %>').childNodes;
for(i = 0; i < list.length; i++)
if(list[i].type == "radio")
if(list[i].checked) return true;
return false;
}
Required.
And for validation summary use following code.Ankit NandekarPosted Apr 28, 2011, 2:25 AM
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
int c = 0;
protected void Page_Load(object sender, EventArgs e)
{
lblMsg.Visible = false;
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (Control chkcontrol in form1.Controls)
{
if (chkcontrol.GetType().ToString() == "System.Web.UI.WebControls.CheckBox")
{
if (((CheckBox)chkcontrol).Checked == true)
{
c++;
// write code when check box checked
}
else
{
if (c == 0)
{
lblMsg.Visible = true;
lblMsg.Text = "please select CheckBox";
}
}
}
}
}
}