Get ValidationGroup of asp.net control in javascript

Scenario:

Updated: Example 1 scenario - I already posted about it earlier here but will leave it here for reference.

Example 1:

I wanted to run Page_ClientValidate upon selection change in a RadioButtonList. I have used jQuery to do some of the finding work. Here is the js code. You can download the sample page here to see it in action.

$(function () {
$('.rbl').change(function () {
var id = $(this).attr('id');
var spans = $('span').filter(function () { return this.controltovalidate == id; });
if (spans.length > 0) {
var validationgroup = spans[0].validationGroup;
//alert(validationgroup);
if (typeof (Page_ClientValidate) == 'function') {
Page_ClientValidate(validationgroup);
}
}
});
});

 

ASP.NET  ValidationControls are rendered as ‘span’. So what the code is doing is getting all spans and then use jquery  ‘filer’ method to filter the spans what has ‘controltovalidate’ equal to the id our RadioButton’s id. Note: ‘controltovalidate’  is exposed as an Expando Property and so we can access it like in the code.

Example 2:

Finally I got a chance to update this post. Here is the code that you need to get the ValidationGroup from Validator Control.

$("#<%=TextBox1.ClientID%>").filter(function(){
if(this.Validators!=null){
var valgroup = this.Validators[0].validationGroup;
//do something with this valgroup
}
});

This technique simply uses the Validators Expando property of the input. It it is not null get the ValidatonGroup of the first validator i.e. this.Validators[0].validatonGroup.

Downloads:

Example 1

Example 2

Conclusion

The above code might not fit every situation but it did fit mine. So enjoy if it works in your case.

No Comments