How you can use a simple CustomValidator control?

Posted by Tripati_tutu under ASP.NET category on | Points: 40 | Views : 2556
You can use the CustomValidator control for textboxes. For example
<h1>Sample CustomValidator:</h1><br />
<asp:TextBox ID="txtValue" runat="server" />
<asp:CustomValidator ID="cstVal" runat="server" ControlToValidate="txtValue" OnServerValidate="cstVal_Validation" ErrorMessage="Please try to enter the value that is not more than 10 characters" />
<br />

The work of this above validation control is to check the input values are alphabates or not.
Here I have mentioned OnServerValidate event. It is used to reference a method from CodeBehind which will help to handle the validation on server. So write the below code in your code behind file.
protected void cstVal_Validation(object sender, ServerValidateEventArgs e)
if(text.Value.Length <= 10)
text.IsValid = true;
text.IsValid = false;
Here the "text" is a variable for the text value that you are getting from the textbox.

Comments or Responses

Login to post response