views:

574

answers:

3

Hi, I am trying to disable validators using jquery.

I have already looked http://stackoverflow.com/questions/400346/disable-asp-net-validators-with-javascript and couple of others doing the same.

It seems be working but its breaking.

My code:

$('.c_MyValdiators').each(function() {

    var x = $(this).attr('id');
    var y = document.getElementById(x);
    ValidatorEnable(y[0], false);
});

I get Error: val is undefined [Break on this error] val.enabled = (enable != false);\r\n

Alternatively if I use

$('.c_MyValdiators').each(function() {
    ValidatorEnable($(this), false); OR ValidatorEnable($(this[0]), false);
  });

I get Error:

val.style is undefined [Break on this error] val.style.visibility = val.isvalid ? "hidden" : "visible";\r\n

Any idea or suggestions?

+2  A: 

I beleive that ValidatorEnable takes the ASP.net ID rather that the ClientID produced by ASP.net. You will also need to make the validation conditional in the CodeBehind.

here is an example:

Of particular use is to be able to enable or disable validators. If you have validation that you want active only in certain scenarios, you may need to change the activation on both server and client, or you will find that the user cannot submit the page.

Here is the previous example with a field that should only be validated when a check box is unchecked:

public class Conditional : Page {
    public HtmlInputCheckBox chkSameAs;
    public RequiredFieldValidator rfvalShipAddress;
    public override void Validate() {
        bool enableShip = !chkSameAs.Checked;
        rfvalShipAddress.Enabled = enableShip;
        base.Validate();
    }
}

Here is the client-side equivalent:

<input type=checkbox runat=server id=chkSameAs 
   onclick="OnChangeSameAs();" >Same as Billing<br>
<script language=javascript>
function OnChangeSameAs() {
    var enableShip = !event.srcElement.status;
    ValidatorEnable(rfvalShipAddress, enableShip);
}
</script>

Reference: http://msdn.microsoft.com/en-us/library/aa479045.aspx

Russ Bradberry
Thanks for your reply. The MSDN link is good for future reference. I dont have any validate() to override since I m in ascx file. I just got it working after so many tries though using:ValidatorEnable(document.getElementById($(this).attr('id')), true);There is one more problem, Since this just enabling/disabling on the client side, how can I disable my validators as such they are disabled when the page postbacks? any ideas?
LibraRocks
you may want to just create a CustomValidator with a custom clientside script that checks based on a condition. The link i posted above shows how to do this as well
Russ Bradberry
A: 
ValidatorEnable(document.getElementById($(this).attr('id')), true);
LibraRocks
A: 

I just stumbled upon your Question [a year later]. I too wanted to disable all validators on a page using JQuery here is how I handled it.

$('span[evaluationfunction]').each(function(){ValidatorEnable(this,false);});

I look for each span on the page that has the evaluatefunction attribute then call ValidatorEnabled for each one of them.

I think the $('this') part of your code is what was causing the hickup.

THarris76