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);
    }
2
  • Do you know the controls for which to bypass validation ? Commented Jul 14, 2016 at 2:37
  • stackoverflow.com/questions/9704039/… Commented Jul 14, 2016 at 2:48

1 Answer 1

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);
    });
};
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Glad to help you, please don't forget to accept as answer and vote up if this is the case. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.