Validation – CustomValidator


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 !!!

Advertisements

2 thoughts on “Validation – CustomValidator

  1. its not working for me plz heip my code is

    function Valid(source, args) {
    var PS2 = document.getElementById(”).value;

    var PSLN = PS2.length;
    if (PSLN < 7) {

    args.IsValid = true;
    }
    else {
    args.IsValid = false;
    }
    }

    this code show some runtime error like (Microsoft JScript runtime error: ‘undefined’ is null or not an object)

    tel me sr wt can i do

    1. Hi Rohit,
      Problem in statement
      document.getElementById(”).value;
      Please make sure that you are providing proper clientid in getElementByID method. and check whether it return object or not by using following statement :
      Alert (document.getElementById(”);

      Hope this will help !!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s