CustomValidator Example in asp .net

  In this article we will see how to use CustomValidator(Server side and client side). In this example we will ensure that password entered by users cannot be less than 5 characters.

 
Previous article from the series:
 
Step1: Create Textbox and CustomValidator set ControlToValidate and ErrorMessage properties of CustomValidator.

Step2: Create Javascript function to check password length.

Step3: Use ClientValidationFunction property of CustomValidator and enter name of your javascript function here I am using validateLength function.
 
1) Client Side Example of CustomValidator:
 
HTML: 
 
Password(Client side)
 
Javascript Function: 

 
Step4: Create Textbox and CustomValidator set ControlToValidate and ErrorMessage properties of CustomValidator.

Step5: Create server side function to check password length.

Step6: Use OnServerValidate property of CustomValidator and enter name of your server side function here I am using CustomValidator1_ServerValidate function.
 
2) Server Side Example of CustomValidator:
 
HTML: 
   
Password(Server side)
 
 
Javascript Function: 
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length < 5)
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
 
Final output: 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice