Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable asp.net validator using jquery

I am trying to disable validators using jquery.

I have already looked Disable ASP.NET validators with JavaScript and couple of others doing the same.

It seems ot 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?

like image 903
LibraRocks Avatar asked Oct 29 '09 20:10

LibraRocks


People also ask

How to disable Required field Validator in asp net?

Well you can simple use the Enabled="false" property of RequiredFieldValidator .

How to disable RequiredFieldValidator in asp net using jQuery?

FindControl("RequiredFieldValidator1"). ClientID %>')[0], false); ValidatorEnable($('#<%=WizardStep1.

How to remove Required field Validator in asp net using JavaScript?

So I can disable the requiredFieldValidators in javascript by calling this... ValidatorEnable(document. getElementById(validatorId), false);

How to disable Required field Validation in mvc?

Use ValidatorEnable function from the Asp.net javacsript Script Library to Enable/Disable the validators on client side. Sometimes you may need to enable/disable validators on client side. you can easily do this using ValidatorEnable function in the Asp.net javacsript Script Library.


2 Answers

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

like image 195
Russ Bradberry Avatar answered Oct 23 '22 12:10

Russ Bradberry


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.

like image 20
Troy Harris Avatar answered Oct 23 '22 13:10

Troy Harris