Control [ID] referenced by the ControlToValidate property of [ID] cannot be validated.


The above error will come when you are trying to validate asp.net checkbox with requiredfieldvalidator or any other validator by giving controltovalidate property as checkbox ID,

To Resolve this we need to validate checkbox without giving any Id as control to validate, so we need to use CustomValidator.

CustomValidator Doesn’t need any id, it needs only clientvalidationfunction or servervalidationfunction…

so the problem is solved…

Click this Link to get Solved Code

Advertisements

How to validate CheckBox Using Custom Validator


Write the code in .aspx page as follows…

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function ClientValidationForCheckBox(sender, args) {
        var CheckBox1 = document.getElementById("<%=CheckBox1.ClientID %>")
            if (CheckBox1.checked == true) {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please Select CheckBox"
         ClientValidationFunction="ClientValidationForCheckBox"></asp:CustomValidator>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>

OutPut

Image

RangeValidator For Date To allow Only Past One Month


Sometimes we need to allow date in textbox only from past one month for that follow this code

First place a scriptmanager in webform and write following code

Add Registration Tag:

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

Code in .aspx Page:

<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
<asp:CalendarExtender ID="TextBox1_CalendarExtender" runat="server"
Enabled="True" TargetControlID="TextBox1">
</asp:CalendarExtender>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ErrorMessage="Select Date From Past One Month Only" ControlToValidate="TextBox1" Type="Date"></asp:RangeValidator>
<asp:ValidatorCalloutExtender ID="RangeValidator1_ValidatorCalloutExtender"
runat="server" Enabled="True" TargetControlID="RangeValidator1">
</asp:ValidatorCalloutExtender>

Code in .aspx.cs file:

protected void Page_Load(object sender, EventArgs e)
{
RangeValidator1.MaximumValue = RangeValidator2.MaximumValue = System.DateTime.Now.ToShortDateString();
RangeValidator1.MinimumValue = RangeValidator2.MinimumValue = System.DateTime.Now.AddDays(-30).ToShortDateString();
}

The ValidationSummary Server Control in asp.net


The ValidationSummary Server Control:

The ValidationSummary control is not a control that performs validations on the content input into your Web forms. Instead, this control is the reporting control, which is used by the other validation controls on a page. You can use this validation control to consolidate error reporting for all the validation errors that occur on a page instead of leaving this up to each and every individual validation control.

A partial page example of the ValidationSummary control

        <p>First name
   <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
    &nbsp;
   <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
    Runat="server" ErrorMessage="You must enter your first name"
    ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</p>
<p>Last name
   <asp:TextBox ID="TextBox2" Runat="server"></asp:TextBox>
    &nbsp;
   <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
    Runat="server" ErrorMessage="You must enter your last name"
    ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
</p>
<p>
   <asp:Button ID="Button1" OnClick="Button1_Click" Runat="server"
    Text="Submit"></asp:Button>
</p>
<p>
   <asp:ValidationSummary ID="ValidationSummary1" Runat="server"
    HeaderText="You received the following errors:">
   </asp:ValidationSummary>
</p>
<p>
   <asp:Label ID="Label1" Runat="server"></asp:Label>
</p>

In addition to bulleted lists, you can use the DisplayMode property of the ValidationSummary control to change the display of the results to other types of formats. This control has the following possible values:

  • BulletList
  • List
  • SingleParagraph

You can also utilize a dialog box instead of displaying the results to the Web page.

 Using a dialog box to report validation errors

<asp:ValidationSummary ID="ValidationSummary1" Runat="server"
        ShowMessageBox="True" ShowSummary="False"></asp:ValidationSummary>

Using Images and Sounds for Error Notifications:

An interesting tip regarding the validation controls is that you are not limited to just text — you can also use images and sounds for error notifications.

To do this, you use the ErrorMessage property of any of the validation controls. To use an image for the error, you can simply place some appropriate HTML as the value of this property.

Using images for error notifications

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" ErrorMessage='<img src="error.gif">' ControlToValidate="TextBox1">
        </asp:RequiredFieldValidator>

Other Related Posts:

Validations:

ASP.NET Validation Server Controls

The RequiredFieldValidator Server Control in asp.net

Validating Drop-Down Lists with the RequiredFieldValidator Control

The CompareValidator Server Control in asp.net

The RangeValidator Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net

The CustomValidator Server Control in asp.net

The ValidationSummary Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net


The RegularExpressionValidator Server Control:

One exciting control that developers like to use is the RegularExpressionValidator control. This control offers a lot of flexibility when you apply validation rules to your Web forms. Using the RegularExpressionValidator control, you can check a user’s input based on a pattern that you define using a regular expression.

For instance, you can define that the structure of the user input must be in the form of an e-mail address or an Internet URL; if it doesn’t match this definition, the page is considered invalid.

 

Making sure the text-box value is an e-mail address

Email:

<asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
  &nbsp;
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
  Runat="server" ControlToValidate="TextBox1"
  ErrorMessage="You must enter an email address"
  ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>

Visual Studio 2005 makes it a little easier to use regular expressions by introducing the Regular Expression Editor. This editor provides a few commonly used regular expressions that you might want to apply to your RegularExpressionValidator. To get at this editor, you work with your page from Design view. Be sure to highlight the RegularExpressionValidator1 server control in this Design view to see the control’s properties. In the Property window of Visual Studio, click the button found next to the ValidationExpression property to launch the Regular Expression Editor.

Using this editor, you can find regular expressions for things like e-mail addresses, Internet URLs, zip codes, phone numbers, and social security numbers. In addition to working with the Regular Expression Editor to help you with these sometimes complicated regular expression strings, you can also find a good-sized collection of them at an Internet site called RegExLib found at www.regexlib.com

Other Related Posts:

Validations:

ASP.NET Validation Server Controls

The RequiredFieldValidator Server Control in asp.net

Validating Drop-Down Lists with the RequiredFieldValidator Control

The CompareValidator Server Control in asp.net

The RangeValidator Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net

The CustomValidator Server Control in asp.net

The ValidationSummary Server Control in asp.net

The RangeValidator Server Control in asp.net


The RangeValidator Server Control:

The RangeValidator control is quite similar to that of the CompareValidator control, but it makes sure that the end user value or selection provided is between a specified range as opposed to being just greater than or less than a specified constant. For an example of this, go back to the text-box element that asks for the age of the end user and performs a validation on the value provided.

 

Using the RangeValidator control to test an integer value

Age:

<asp:TextBox ID=”TextBox1″ Runat=”server”></asp:TextBox>

  &nbsp;

<asp:RangeValidator ID=”RangeValidator1″ Runat=”server”

  ControlToValidate=”TextBox1″ Type=”Integer”

  ErrorMessage=”You must be between 30 and 40″

  MaximumValue=”40″ MinimumValue=”30″></asp:RangeValidator>

Other Related Posts:

Validations:

ASP.NET Validation Server Controls

The RequiredFieldValidator Server Control in asp.net

Validating Drop-Down Lists with the RequiredFieldValidator Control

The CompareValidator Server Control in asp.net

The RangeValidator Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net

The CustomValidator Server Control in asp.net

The ValidationSummary Server Control in asp.net

The CompareValidator Server Control in asp.net


The CompareValidator Server Control:

The CompareValidator control allows you to make comparisons between two form elements as well as to compare values contained within form elements to constants that you specify. For instance, you can specify that a form element’s value must be an integer and greater than a specified number. You can also state that values must be strings, dates, or other data types that are at your disposal.

One of the more common ways of using the CompareValidator control is to make a comparison between two form elements. For example, suppose that you have an application which requires users to have passwords in order to access the site. You create one text box asking for the user’s password and a second text box which asks the user to confirm the password. Because the text box is in password mode, the end user cannot see what she is typing — just the number of characters that she has typed. To reduce the chances of the end user mistyping her password and inputting this incorrect password into the system, you ask her to confirm the password. After the form is input into the system, you simply have to make a comparison between the two text boxes to see if they match. If they match, it is likely that the end user typed the password correctly, and you can input the password choice into the system. If the two text boxes do not match, you want the form to be invalid.

Using the CompareValidator to test values against other control values

<%@ Page Language="C#" %>
<script runat="server">
    protected void Button1_Click(Object sender, EventArgs e) {
       Label1.Text = "Passwords match";
    }
    </script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CompareFieldValidator</title>
</head>
<body>
    <form runat="server">
        <p>
            Password<br>
            <asp:TextBox ID="TextBox1" Runat="server"
             TextMode="Password"></asp:TextBox>
             &nbsp;
            <asp:CompareValidator ID="CompareValidator1"
             Runat="server" ErrorMessage="Passwords do not match!"
             ControlToValidate="TextBox2"
             ControlToCompare="TextBox1"></asp:CompareValidator>
        </p>
        <p>
            Confirm Password<br>
            <asp:TextBox ID="TextBox2" Runat="server"
             TextMode="Password"></asp:TextBox>
        </p>
        <p>
            <asp:Button ID="Button1" OnClick="Button1_Click"
             Runat="server" Text="Login"></asp:Button>
        </p>
        <p>
            <asp:Label ID="Label1" Runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>

Looking at the CompareValidator control on the form, you can see that it is similar to the RequiredFieldValidator control. The CompareValidator control has a property called ControlToValidate that associates itself with one of the form elements on the page. In this case, you need only a single CompareValidator control on the page because a single comparison is made. In this example, you are making a comparison between the value of TextBox2 and that of TextBox1. Therefore, you use the ControlToCompare property. This specifies what value is compared to TextBox2. In this case, the value is TextBox1.

Validating against Constants

Besides being able to validate values against values in other controls, you can also use the CompareValidator control to make comparisons against constants of specific data types.

 

Using the CompareValidator to validate against constants

Age:

<asp:TextBox ID=”TextBox1″ Runat=”server” MaxLength=”3″>

</asp:TextBox>

&nbsp;

<asp:CompareValidator ID=”CompareValidator1″ Runat=”server”

  ErrorMessage=”You must enter a number”

  ControlToValidate=”TextBox1″ Type=”Integer”

  Operator=”DataTypeCheck”></asp:CompareValidator>

To specify the data types that you want to use in these comparisons, you simply use the Type property. The Type property can take the following values:

  • Currency
  • Date
  • Double
  • Integer
  • String

Not only can you make sure that what is entered is of a specific data type, but you can also make sure that what is entered is valid when compared to specific constants. For instance, you can make sure what is entered in a form element is greater than, less than, equal to, greater than or equal to, or less than or equal to a specified value.

 Making comparisons with the CompareValidator control

Age:

<asp:TextBox ID=”TextBox1″ Runat=”server”></asp:TextBox>

  &nbsp;

<asp:CompareValidator ID=”CompareValidator1″ Runat=”server”

  Operator=”GreaterThan” ValueToCompare=”18″

  ControlToValidate=”TextBox1″

  ErrorMessage=”You must be older than 18 to join” Type=”Integer”>

</asp:CompareValidator>

The Operator property can take one of the following values:

  • Equal
  • NotEqual
  • GreaterThan
  • GreaterThanEqual
  • LessThan
  • LessThanEqual
  • DataTypeCheck

Other Related Posts:

Validations:

ASP.NET Validation Server Controls

The RequiredFieldValidator Server Control in asp.net

Validating Drop-Down Lists with the RequiredFieldValidator Control

The CompareValidator Server Control in asp.net

The RangeValidator Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net

The CustomValidator Server Control in asp.net

The ValidationSummary Server Control in asp.net