Sunday, November 2, 2008

ASP.net Custom Validation

ASP.net validation control , such as RequiredFieldValidator , RangeValidator and CompareValidator , is very useful.

But , there will be a day that we need to write a javascript function and used it as CustomValidation.

This is an example of CustomValidation that validate the value entered in a textbox.

The script will need 2 parameters , which will be supplied by CustomValidator control



function CustomValidation(ValidateResult,ObjectToValidate) {
if (ObjectToValidate.Value >= 1 && ObjectToValidate.Value <= 10) {
ObjectToValidate.IsValid = true;
}
else {
ValidateResult.innerText = "Please enter value in range of 1-10 only";
ObjectToValidate.IsValid = false;
}
}



Add a CustomValidator , point ControlToValidate to a textbox and set ClientValidationFunction as "CustomValidation"