CustomValidator problem

1.9k Views Asked by At

I have a number of ASP.NET validators on my page of various types, some of which I am disabling depending on user selections on a page, using javascript.

$.each(Page_Validators, function(index, validator) 
       {
           if ($(validator).hasClass("pain")) {

               ValidatorEnable(validator, false);

        }
});

This seems to work and validators do not fire in all cases except any CustomValidators that I am using whenever I cannot use other validator types

 <asp:CheckBoxList id="BodyPartsList" runat="server" RepeatColumns = "2" 
                    Width="1023px">
                    <asp:ListItem Text = "0. Head/headaches" Value = "1"></asp:ListItem>
                    <asp:ListItem Text = "1. Leg(s)" Value = "2"></asp:ListItem>
                    <asp:ListItem Text = "2. Arm(s)" Value = "3"></asp:ListItem>
                    <asp:ListItem Text = "3. Neck" Value = "4"></asp:ListItem>
                    <asp:ListItem Text = "4. Shoulder(s)" Value = "5"></asp:ListItem>
                    <asp:ListItem Text = "5. Low Back" Value = "6"></asp:ListItem>
                    <asp:ListItem Text = "6. Upper Back" Value = "7"></asp:ListItem>
                    <asp:ListItem Text = "7. Feet" Value = "8"></asp:ListItem>
                    <asp:ListItem Text = "8. Hand(s)" Value = "9"></asp:ListItem>
                    <asp:ListItem Text = "9. Other(Describe in &quot;Details of Plan&quot;)" Value = "10"></asp:ListItem>
                </asp:CheckBoxList>

                <asp:CustomValidator ID="PainLocationValidator" runat="server" Display="Dynamic"
                ErrorMessage="Location of pain is required." 
                ClientValidationFunction="checkPainListValidate" 
                ValidationGroup = "OnSave" EnableClientScript= "true" SetFocusOnError= "true" Width = "100%" CssClass = "pain" />



 function checkPainListValidate(sender, args) {

    args.IsValid = true;

    if ($('#<%= BodyPartsList.ClientID %> input:checked').length > 0)
        args.IsValid = true;

    else
        args.IsValid = false;

}

Why is this happening? Is there something else I can do to disable those as well?

Thanks.

1

There are 1 best solutions below

9
On BEST ANSWER

Some aspx-markup would be helpful, but anyway here are some ideas:

  • have you debugged the javascript to see what's happening, if your validator is null or you have a type like validatorEnable instead of ValidatorEnable? Do you get any javascript errors?
  • Does the CustomValidator validates on server- or on clientside, i think ValidatorEnable will only disable it on clientside
  • set EnableClientScript to true
  • If you dont specify a ControlToValidate, the CustomValidator will validate on every postback, regardless of the event that caused it
  • Have you provided a ClientValidationFunction that gets called on clientside?
  • by the way, what browser and what framework are you using, is it ASP.Net-Ajax?
  • do you have tried to disable it via document.getElementById('<%=MyValidator.ClientID %>').disabled=true;?

I assume that you're using the wrong ID to get the validator in clientside.

I have tested your code and can not reproduce this behaviour:

This is my Javascript-function to disable all validators(edited to take your pain-class into account):

 function disablePainValidators() {
     if ((typeof (Page_Validators) != "undefined") && (Page_Validators != null)) {
         var i;
         for (i = 0; i < Page_Validators.length; i++) {
             if(Page_Validators[i].className=='pain')
                 ValidatorEnable(Page_Validators[i], false);
         }
     }
 }