0

I am trying to disable some requiredfield validators in an ASP.NET page. I have written following javascript function. I have four required field validators. The selector works fine but when it comes to disabling validation it fails. However if I change the for loop to for (var j = 0; j < validators.length-1; j++) the ValidatorEnable works for three i.e. I can disable them easily. Can anyone please let me know why I can't disable the last validator i.e. the fourth one.

function disableValidation() {
        var validators = $("[id^=MainContent_MemberInitiateRolloverControl1_][id$=_vr]");
        for (var j = 0; j < validators.length; j++) {
            console.log(validators[j]);
            ValidatorEnable(validators[j], false);
    }
Shalin
  • 157
  • 1
  • 6

1 Answers1

0

Set the CausesValidation property to false to the controls. RequiredFieldValidator wouldn't validate them. an example:

<asp:TextBox ID="txt" runat="server" Text="New" CausesValidation="False" />

JavaScript solution

if (Page_Validators) {
    PageValidators.forEach(function(pageValidator) {
        if (pageValidator == null) {return;}
        if (pageValidator.controltovaliddate != "<%= ControlToValidate.ClientID %>") {
            return;
        }
        ValidatorEnable(pageValidator, false);
    });
};
Sami
  • 3,686
  • 4
  • 17
  • 28
  • I was doing the same thing just the way I implemented it was different. I don't know what went wrong. I didn't have to make a single change to get the code working. (My own code worked for me) Anyways thanks. – Shalin Jul 15 '16 at 04:46
  • Glad to help you, please don't forget to accept as answer and vote up if this is the case. Thank you – Sami Jul 15 '16 at 04:49