36

I am working with a page and I am getting an Page.IsValid = false and I am trying to determine which control is causing the validation problem.

j0k
  • 22,600
  • 28
  • 79
  • 90
Robert
  • 1,129
  • 2
  • 12
  • 23

4 Answers4

28

Credit to Steven for this answer, but I had to make some changes for it to work as this.Validators.Where() had some problems.

using System.Linq;

List<IValidator> errored = this.Validators.Cast<IValidator>().Where(v => !v.IsValid).ToList();
Community
  • 1
  • 1
Ozziereturnz
  • 488
  • 4
  • 8
  • 3
    it didn't work the way @Steven said, but this answer is perfect. Only had to change the *this* for *Page*. thks – StinkyCat Aug 07 '14 at 10:08
  • Instead of using this.Validators I am using Page.Validators – JohnnyBizzle Jul 09 '15 at 16:40
  • `Page.Validators` didn't work for me.. yes, somebody out there is still using WebForms... bleh. But this answer worked fine. Thanks +1 – Piotr Kula Aug 13 '15 at 15:55
  • Building on this answer, I use the following code to identify the validator that is invalid: 'if (Page.IsValid) { // do something } else { // page is not valid List invalides = this.Validators.Cast().Where(v => !v.IsValid).ToList(); foreach (var invalid in invalides) { Debug.WriteLine(invalid.ErrorMessage); } }' – CAK2 Jan 05 '18 at 05:56
24

In code (page_load), you can do this:
(per MSDN: http://msdn.microsoft.com/en-US/library/dh9ad08f%28v=VS.80%29.aspx)

If (Me.IsPostBack) Then
    Me.Validate()
    If (Not Me.IsValid) Then
        Dim msg As String
        ' Loop through all validation controls to see which 
        ' generated the error(s).
        Dim oValidator As IValidator
        For Each oValidator In Validators
            If oValidator.IsValid = False Then
                msg = msg & "<br />" & oValidator.ErrorMessage
            End If
        Next
        Label1.Text = msg
    End If
End If

In the markup, you can...

  • You can put "text" on your validator (like an asterisk...)
  • Or use a validation_summary control (which requires an error message on your validator)...
Chains
  • 12,541
  • 8
  • 45
  • 62
  • 21
    Or `this.Validators.Where(v => !v.IsValid)` for short. – Steven Aug 01 '11 at 16:17
  • 1
    @Steven -- I keep meaning to learn about that -- very cool though -- you should get credit for that. – Chains Aug 01 '11 at 16:19
  • 2
    It is actually very easy to give me credits for that. Just click on the 'arrow up' simple on the left side of my comment ;-). – Steven Aug 01 '11 at 19:28
  • 2
    @Steven_ That doesn't compile because a `ValidatorCollection` doesn't implement `IEnumerable` but you could use `var v = Validators.Cast().Where(v => !v.IsValid);` – Tim Schmelter Jun 18 '18 at 13:04
8

The accepted answer allows you to find the validation message of the validator that failed. If you want to find the ID of the control that failed validation, it is possible to get that by casting the validator to a BaseValidator which exposes the ControlToValidate property. For example:

For Each v As BaseValidator In Page.Validators
    If Not v.IsValid Then
        ' You can see the control to validate name and error message here.
        Debug.WriteLine(v.ControlToValidate)
        Debug.WriteLine(v.ErrorMessage)
    End If
Next
NYCdotNet
  • 4,500
  • 1
  • 25
  • 27
2

To check which Validator is being fired, just check the HTML in Firebug and if any Validator is not having display:none; property or is having visibility:visible in its properties then it is the one which is causing Page.IsValid false.

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
Nitin
  • 41
  • 1