Tutorial :Checking if a ValidationGroup is valid from code-behind



Question:

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")  


Solution:1

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;  }  


Solution:2

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.


Solution:3

var isValidGroup = Page                      .GetValidators(sValidationGroup)                      .Cast<IValidator>()                      .All(x => x.IsValid);  


Solution: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.


Solution:5

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;      }  


Solution:6

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;      }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »