Using the CustomValidator Control

CustomValidator control provides the customize validation code to perform both client-side and server-side validation. For example, you can create a validation control that checks whether the value entered into a text box is 8 or more characters long. If none of the other validation controls perform the type of validation that you need, you can always use the CustomValidator control. The control allows you to validate both clientside and serverside, where the serverside approach is probably the most powerful. You can associate your custom validation function with the CustomValidator control by handling the ServerValidate event. Code
< asp:TextBox runat="server" id="txtCustomValidator" / > < asp:CustomValidator runat="server" id="cusCustomTxt" controltovalidate="txtCustomValidator" onservervalidate="cusCustomTxt_ServerValidate" errormessage="Lebgth must be 8 or more characters..." / >
C# Code
protected void cusCustomTxt_ServerValidate(object sender, ServerValidateEventArgs e) { if(e.Value.Length >= 8) e.IsValid = true; else e.IsValid = false; }
If you don't associate a client validation function with a CustomValidator control, then the CustomValidator doesn't render the error message until you post the page back to the server. Because the other validation controls prevent a page from being posted if the page contains any validation errors , you won't see the error message rendered by the CustomValidator control until you pass every other validation check in a page.