Tuesday, June 3, 2008

Client-side Custom Validators

When using custom validators in ASP.NET, you'll probably notice that all of the validation occurs only on postback. This is because the only code present is on the server-side. You can write up your own Javascript function to perform client-side validation and assign it to the ClientValidationFunction property of the custom validator.

The validation function has to use the signature function ClientsideValidationScript(source, args).
You would have to set the IsValid member of the args parameter to true or false, depending on the result of the check condition.

Example:

function Scriptology(source, args)
{
var x = document.getElementById('txtNitin').value;
if (x > 5)
args.IsValid = true;
else
args.IsValid = false;
}

No comments: