ASP.Net CustomValidator Example, How to <asp:CustomValidator> Control customized validation logic in asp.net c#

<asp:CustomValidator
id="ProgrammaticID"
ControlToValidate="programmatic ID of Server Control to Validate"
ClientValidationFunction="ClientValidateID"
OnServerValidate="ServerValidateID"
ErrorMessage="Message to display in ValidationSummary control"
Text="Message to display in control"
ForeColor="value"
BackColor="value"
runat="server" >
</asp:CustomValidator>

The CustomValidator control allows you to create a validation control with customized validation logic. For example, you can create a validation control that checks whether the value entered into a text box is an even number.

Validation controls always perform validation checking on the server. They also have complete client-side implementation that allows DHTML-supported browsers (such as Microsoft Internet Explorer 4.0 or later) to perform validation on the client. Client-side validation enhances the validation process by checking user input before it is sent to the server. This allows errors to be detected on the client before the form is submitted, avoiding the round-trip of information necessary for server-side validation.


Check the length of the string in the TextBox. This is a very basic example, to show how you may use the CustomValidator.

<asp:TextBox runat="server" id="txtExample" />
<asp:CustomValidator runat="server" id="CustomValidatorExample" controltovalidate="txtExample" onservervalidate="CustomValidatorExample_ServerValidate" errormessage="The text must be exactly 10 characters long!" />

protected void CustomValidatorExample_ServerValidate(object sender, ServerValidateEventArgs e)
{
if(e.Value.Length == 10)
e.IsValid = true;
else
e.IsValid = false;
}
Tags: ,
Hot on Web:


About author