Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

Posts Tagged ‘Validation – CustomValidator’

Validation – CustomValidator

Posted by Ramani Sandeep on October 3, 2008

In this example, we will simply check the length of the string in the TextBox. This is a very basic and that useful example, only made to show you how you may use the CustomValidator.

<asp:TextBox runat="server" id="txtCustom" />

<asp:CustomValidator runat="server" id="cusCustom" 

    controltovalidate="txtCustom" 

    onservervalidate="cusCustom_ServerValidate" 

    errormessage="The text must be exactly 8 characters long!" />

As you can see, it’s pretty simple. The only unknown property is the onservervalidate event. It’s used to reference a method from CodeBehind which will handle the validation. Switch to our CodeBehind file and add the following method:

protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)

{

    if(e.Value.Length == 3)

        e.IsValid = true;

    else

        e.IsValid = false;

}

Hope this will Help you !!!

Posted in ASP.NET | Tagged: , | 2 Comments »

 
Follow

Get every new post delivered to your Inbox.

Join 317 other followers