ASP.NET - showing error in summary validation

Asked By Anjali agarwal
09-Feb-12 11:56 PM
Hi All,

  I have a checkboxlist that I am binding from code behind. I want the user to check at least one check box and if the user does not check any boxes, i want to display an error as part of the validation summary.

How can I accomplish this.

Thanks.

  Danasegarane Arunachalam replied to Anjali agarwal
10-Feb-12 12:01 AM
You need to use the compare validator with javascript for this functionality.


Here is the example


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>CheckBox vadliation</title>

<script type="text/javascript">

 

<!--

 
function CheckCheckBox(source, args) {

var cb = document.getElementById('CheckBox1');
if ( cb.checked == true ) {

args.IsValid = true;
} else {args.IsValid = false;

}

}

 

-->

 
</script>

</head>

<body>

<form id="form1" runat="server">

<div>

 

<asp:CheckBox ID="CheckCheckBox" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"

ClientValidationFunction="CheckCheckBox"

ErrorMessage="* Check any of the  checkbox for postback"></asp:CustomValidator>

<br />

<asp:Button ID="Button1" runat="server" Text="Button" />

 

</div>
</form>

</body>

</html>

  D Company replied to Anjali agarwal
10-Feb-12 12:04 AM
Hello Anjali,

As u have mentioned and we know that check boxes have the property know as checked, by using this in javascript we can validate it easily, something in this way.

1.)find the control inside which all the checkboxes are placed

than use a for loop to go through all the checkboxes.
Save all controls in an array get the length (no. of conrols availiable)
for (var i = 0; i < chkBoxes.length; i++)
{
if (chks[i].checked)
{
return true;
}
else
{alert('msg')return false;}


Hope its clear
Regards
D
  Somesh Yadav replied to Anjali agarwal
10-Feb-12 12:08 AM
Hi anjali as per your requirement we can do it as follows. you just gothrough the below example you will understand how to proceed further.

Validating a Checkbox Field

There is no RequiredValidator however for the Checkbox control. In order to check whether a checkbox was ticked, you need to use a CustomValidator. The CustomValidator allows you to create custom rules for validation that can’t be satisfied by the other validator controls.

CssClass=”FieldError”Display=”Dynamic”ForeColor=”Red”
ValidationGroup=”ValidateForm”OnServerValidate=”cv1_ServerValidate”>
You must agree to the terms to register

I agreeto the terms &conditions

protected void cv1_ServerValidate(object source,�
            ServerValidateEventArgs args)�
{�
    args.IsValid = terms.Checked;�
}

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.servervalidateeventargs.aspx has two properties, IsValid and Value. In our example, the IsValid state is set according to whether the checkbox is ticked. The Value property is used for validating controls such as a TextBox where it will contain the value in the field.

Finally. in the code that performs what ever action you need after the submit button is pressed you simply check for Page.IsValid.

I hope this helps you a lot.

If possible please gothrough the below example also

Drawbacks of Server Side Validation for a Checkbox Field

The problem with this approach, is that the Checkbox validation occurs server side, while all your other validation occurs client side. This means that when a customer clicks submit without filling out the form properly, the Name and Email validators will fire, but no message will appear if the checkbox is not ticked.

If the name and email address fields are filled in correctly then the form will postback on submit, and then return with a message saying that the check box is not ticked.
To get around this, we add a client side check to the Checkbox by adding ClientValidationFunction=AgreeTerms:

    CssClass="FieldError" Display="Dynamic" ForeColor="Red"�
    ValidationGroup="ValidateForm" OnServerValidate="cv1_ServerValidate"�
    ClientValidationFunction="AgreeTerms">

 

And then at the top of the page, add the following function:


Now all the controls validate on the client side, and as an added bonus, the checkbox will check on the server side if the client has Javascript disabled.

Create New Account
help
Net hi friends Any one send frequently asked Important questions in C# .Net, ADO .Net, Asp .Net and Sql Server. . . . . . . . tx in Advance. . . . . . Hi, Find this. . (B)What is an IL? (B A) What is scavenging? (B) What are different types of caching using cache object of ASP.NET? (B) How can you cache different version of same page using ASP.NET cache object? (A) How will implement Page Fragment Caching? (B) Can you compare ASP.NET sessions with classic ASP? (B) Which are
in Sql server as var_name int How do you separate business logic while creating an ASP.NET application? There are two level of asp.net debugging 1. Page level debugging For this we have to edit the page level debugging enable the trace to true in the line in the html format of the page. %@ Page Language = ”vb” trace = ”true”AutoEventWireup = ”false” Codebehind = ”WebForm1.aspx.vb” Inherits = ”WebApplication2.WebForm1?&gt; 2
Help me. . . . . . hi all, i just want to say that actually i am new to asp.net 2.0 i have a work over my head and really m not knowing how ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] I have one Login Form which contains useriD(textbox), password(textbox), role(in role we have two type of Employees one is User and other reporting dropdown control is used and login button and one employee Profile Form which contains EmpID(textbox), Empname(textbox), MysiteURL(textBox), Reporting officer(textbox), E-mail(textbox) and Resume(file upload control) and edit button is used EmployeeView Form
Using the ASP.NET CustomValidator Control The CustomValidator evaluates the value of an input control to determine whether it the input control to validate can be accessed by using the Value property of the ServerValidateEventArgs object passed into the event handler as a parameter. The result of the validation is then stored in the IsValid property of the ServerValidateEventArgs object. To create a client-side validation function, first add the server-side validation function Next, add the client-side validation script function to the .aspx page markup. The function must be in the form: Function ValidationFunctionName (source, arguments) Use the ClientValidationFunction control to validate different criteria. For example, you can apply multiple validation controls on a TextBox control that allows the user to enter the quantity of items to add to a inventory and a RequiredFieldValidator control to ensure that the user enters a value into the TextBox control. If the input control is empty, no validation functions are called and validation succeeds the user from skipping an input control. One of the big advantages of using these ASP.NET validation controls instead of custom-written javascript is that they emit some highly customized
Validate listbox in asp.net Validate listbox in asp.net Use this function function ListBoxValid(sender, args) { var listBox = document.getElementById(sender.controltovalidate); var listBoxCnt handler, and then you would implement server side like this: protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { args.IsValid = ListBox1.Items.Count > 0; } HI try this < asp : ListBox ID = "ListBox1" runat asp : Button ID = "Button1" runat = "server" Text = "Button" / > refer http: / / www.aspsnippets.com / Articles / Validating-ASP.Net-ListBox-Control-client-side-using-Custom-Validator.aspx HI try this <%@ Page Language = "C#" %> <!DOCTYPE html PUBLIC "- / / W3C / / DTD XHTML 1.0 Transitional / / EN" " http: / / www.w3