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:
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:
Javascript Function:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (args.Value.Length < 5)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
Final output: