27

Is there a method I can call that retrieves a boolean value of whether or not a particular ValidationGroup is valid? I don't want to actually display the validation message or summary - I just want to know whether it is valid or not.

Something like:

Page.IsValid("MyValidationGroup")

6 Answers 6

38

Have you tried using the Page.Validate(string) method? Based on the documentation, it looks like it may be what you want.

Page.Validate("MyValidationGroup");
if (Page.IsValid)
{
    // your code here.
}

Note that the validators on the control that also caused the postback will also fire. Snip from the MSDN article...

The Validate method validates the specified validation group. After calling the Validate method on a validation group, the IsValid method will return true only if both the specified validation group and the validation group of the control that caused the page to be posted to the server are valid.

1
  • It's worth to mention this will display validation messages on failed validations. Aug 13, 2014 at 10:25
16
protected bool IsGroupValid(string sValidationGroup)
{
   foreach (BaseValidator validator in Page.Validators)
   {
      if (validator.ValidationGroup == sValidationGroup)
      {
         bool fValid = validator.IsValid;
         if (fValid)
         {
            validator.Validate();
            fValid = validator.IsValid;
            validator.IsValid = true;
         }
         if (!fValid)
            return false;
      }

   }
   return true;
}
1
  • 2
    +1 You can also use Page.GetValidators("<your group name>") which would simplify code a small bit.
    – James
    Aug 1, 2011 at 22:08
8
var isValidGroup = Page
                    .GetValidators(sValidationGroup)
                    .Cast<IValidator>()
                    .All(x => x.IsValid);
4

Try this:

Page.Validate("MyValidationGroup");
if (Page.IsValid) 
{
    //Continue with your logic
}
else
{
    //Display errors, hide controls, etc.
}

Not exactly what you want, but hopefully close.

2
  • So will Page.Validate cause my ValidationSummary to display?
    – Mike Cole
    Jul 28, 2009 at 19:24
  • You would need to programmatically disable/hide the validation summary if you want to use this solution in that manner. Jul 28, 2009 at 19:26
3

Page.IsValid will be false if any of the validated validation groups was invalid. If you want to validate a group and see the status, try:

    protected bool IsGroupValid(string sValidationGroup)
    {
        Page.Validate(sValidationGroup);
        foreach (BaseValidator validator in Page.GetValidators(sValidationGroup))
        {
            if (!validator.IsValid)
            {
                return false;
            }
        }
        return true;
    }
1

Pavel's answer works but isn't the simplest. Here is how I solved it:

protected Boolean validateGroup(String validationGroupName) {
        Boolean isGroupValid = true;

        foreach (BaseValidator validatorControl in Page.GetValidators(validationGroupName)) {
            validatorControl.Validate();

            if (!validatorControl.IsValid)
                isGroupValid = false;
        }

        if (!isGroupValid)
            return false;
        else
            return true;
    }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.