How to: Test Validity Programmatically for ASP.NET Server Controls

Validation controls test user input, set an error state, and produce error messages. They do not change the flow of page processing—for example, they do not bypass your code if they detect a user input error. Instead, you test the state of the controls in your code before performing application-specific logic. If you detect an error, you prevent your own code from running; the page continues to process and is returned to the user with error messages.

Security noteSecurity Note:

By default, ASP.NET Web pages automatically validate that malicious users are not attempting to send script or HTML elements to your application. For more information, see Script Exploits Overview.

You can test for a general, page-wide state, and you can test the state of individual controls. Typically, you do these things in the event handlers that you create for a page.

To test for a general error state

  • In your code, test the page's IsValid property. This property rolls up the values of the IsValid properties of all the validation controls on the page (using a logical AND); if any one validation control is set to invalid, the page's property will return false.

    Note

    Validation information is not available during a page's initialization or load stage. However, you can call the Validate method manually during Page_Load and then test the page's IsValid property. For details about page states, see ASP.NET Page Life Cycle Overview.

    The following code example shows the event handler for a button. The code tests the page's IsValid property. Note that there is no need for an else clause, because the page will be returned automatically to the browser and the validation controls will display their own error messages.

    PublicSub Button1_Click(ByVal sender AsObject, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click
    
        IfMe.IsValid Then        ' Perform database updates or other logic hereEndIfEndSub
    
    void Button1_Click(object sender, System.EventArgs e)
    {
        if (IsValid)
        {
            // Perform database updates or other logic here.
        }
    }
    

To test for the error state of individual controls

  • Loop through the page's Validators collection, which contains references to all the validation controls. You can then examine the IsValid property of each validation control.

    Note

    If you want to perform this check during Page_Load, you must manually call the Validate method first.

    The following code example shows how you can get the state of individual validation controls.

    If (Me.IsPostBack) ThenMe.Validate()
        If (NotMe.IsValid) ThenDim msg AsString        ' Loop through all validation controls to see which         ' generated the error(s).Dim oValidator As IValidator
            ForEach oValidator In Validators
                If oValidator.IsValid = FalseThen
                    msg = msg & "<br />" & oValidator.ErrorMessage
                EndIfNext
            Label1.Text = msg
        EndIfEndIf
    
    if (this.IsPostBack)
    {
        this.Validate();
        if (!this.IsValid)
        {
            string msg = "";
            // Loop through all validation controls to see which// generated the errors.foreach (IValidator aValidator inthis.Validators)
            {
                if (!aValidator.IsValid)
                {
                    msg += "<br />" + aValidator.ErrorMessage;
                }
            }
            Label1.Text = msg;
        }
    }
    

See Also

Tasks

How to: Control Validation Error Message Display for ASP.NET Server Controls

How to: Validate Programmatically for ASP.NET Server Controls

Reference

Validating User Input in ASP.NET Web Pages