Monthly Archives: June 2008

Iterate asp.net form validators client side

I added a client side onclick function to a submit button, but wanted to verify that the form validators were all valid before performing my magic.  Unfortunately, you can’t change when the validator logic gets fired, so my onclick function always gets processed before the form validation logic.  Luckily, Microsoft provides a few client side helper methods/properties that allow you to work with the form validators.

You can enable/disable validators via the ValidatorEnable(validator, enable) function, which seems like it would be useful for complicated forms…  But for me, I wanted to iterate the validators, have them perform their logic, and then determine if any of them were invalid.  The following code block will do just that:

var isValid = true;
if (Page_Validators) {
for (var i = 0; i < Page_Validators.length; i++) {
ValidatorValidate(Page_Validators[i]);
isValid = Page_Validators[i].isvalid;
if (!isValid)
break;
}
}

 

To iterate the validators and have them display their message in a validation summary, you’d want to do something like:

var isValid = true;
if (Page_Validators) {
for (var i = 0; i < Page_Validators.length; i++) {
ValidatorValidate(Page_Validators[i]);
isValid = Page_Validators[i].isvalid;
if (isValid)
isValid = Page_Validators[i].isvalid;
}
Page_IsValid = isValid;
ValidationSummaryOnSubmit();
}

 

Hope this helps someone…

Canada 2008

Around the first week of June, I was able to relax in Ontario, Canada with my brother, dad, dad’s friend Larry, and Molly.  It was nice to get away for a few days and not be concerned about email, tv, and cell phones.  One of the cool things about going up there this time of year is that it remains bright from 5am-11pm.  that really makes the guilt of taking a nap pretty much non existent. 🙂  If you’re interested, I posted a few of the pictures here.