Saturday, March 1, 2008

Fire ASP.net client side validation programmatically

This one is really nice. I had a scenario where I wanted to do client side validation, but server side processing was going to be done via a WebMethod call via a JQuery POST. I really like the ASP.net validators for basic validation, so I wanted to use them in this case. Turns out that ASP wires the validation call into the submit javascript somewhere. After a bit of perseverance and some experimentation, I ran across this method:

Page_ClientValidate(validationGroupName)

Turns out that little gem will fire the validators, and return the state of things, which is exactly what I wanted. Here is a basic javascript function that uses it.

function doSave() {
if (Page_ClientValidate("valgroup")) {
alert("valid");
}
}

No comments: