We can add validation controls to asp.net pages to validate user input based on validation criteria.
ASP.NET Validations Controls are
1. RequiredFieldValidator
2. CompareValidator
3. RangeValidator
4. RegularExpressionValidator
5. CustomValidator
Displaying Error Information: Validation error message can be displayed as
Inline: Each validation control can individually display an error message in place i.e, next to the control where the error occurred.
Summary: Validation errors can be collected and displayed in one place
In place and summary: The error message can be different in the summary and in place. We can use this option to show a shorter error message in place with more detail in the summary.
Custom: We can customize the error message display by capturing the error information and designing our own output.
Important properties that are common to all validation controls are:
ControlToValidate: The ID of the control for which the user must provide a value
ErrorMessage: Error Message to be displayed if validation fails (Ex: First name is required)
Display : display mode can be None/Static/Dynamic
- None : Validation message is never displayed
- Static: Space for the validation message is allocated in the page layout
- Dynamic: Space for the validation message is dynamically added to the page if validation fails.
ValidationGroup: We can group validators into groups that are treated as a unit.
1. RequiredFieldValidator : Ensures that the user does not skip an entry
<asp:Textbox id="txtFirstName" runat="server"></asp:Textbox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ControlToValidate="txtFirstName"
ErrorMessage="First name is a required."
>
</asp:RequiredFieldValidator>
2. CompareValidator : Compares a user's entry against a constant value, against the value of another control (using a comparison operator such as less than, equal, or greater than), or for a specific data type
- ControlToCompare: set the control to be compared
- Operator: If we set Operator property to ValidationCompareOperator.DataTypeCheck, the CompareValidator control ignores the ControlToCompare.
- BaseCompareValidator.Type property is used to specify the data type of both comparison values. Both values are automatically converted to this data type before the comparison operation is performed. The following table lists the various data types that can be compared is String, Integer, Double, Date, currency.
- MaximumValue property to set the maximum value
- MinimumValue property to set the minimum value
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
ControlToValidate="txtZip"
ValidationExpression="\d{6}"
Display="Static"
ErrorMessage="Zip code must be 6 numeric digits"
EnableClientScript="False"
runat="server"/>
5. CustomValidator: Checks the user's entry using validation logic that we have to write code ourself.
To create a server-side validation function, provide a handler for the ServerValidate event that performs the validation. The string from 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.
Use the ClientValidationFunction property to specify the name of the client-side validation script function associated with the CustomValidator control. Because the script function is executed on the client.
//Server side
void ServerValidation (object source, ServerValidateEventArgs args)
{
args.IsValid = (CheckBox1.Checked == true);
}
//Client Side
function ValidationFunctionName(source, arguments)
We can attach more than one validation control to an input control for ex: If we want to check textbox value is required and that value with in specified range for that we need to attache requiredfield and range validatior to that textbox control
ValidationSummary control, does not perform validation, but is used to display the error messages from all the validation controls on the page together.
How can you Disable Validation for ASP.NET Server Controls?
Set the control's CausesValidation property to false
<asp:Button id="btnCancel" runat="server"
Text="Cancel" CausesValidation="False">
</asp:Button>
ValidationGroup
You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.
For ex: If I have a page with two textboxes and two buttons I want to validate textbox1 in button1 click and ignore txtbox2 validation and vice versa.