ASP.NET ValidationSummary Control


The ValidationSummary control is used to display a summary of all validation errors occurred in a Web page.

The error message displayed in this control is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed for that validation control.

Properties

Property Description
DisplayMode How to display the summary. Legal values are:
  • BulletList
  • List
  • SingleParagraph
EnableClientScript A Boolean value that specifies whether client-side validation is enabled or not
Enabled A Boolean value that specifies whether the validation control is enabled or not
ForeColor The fore color of the control
HeaderText A header in the ValidationSummary control
id A unique id for the control
runat Specifies that the control is a server control. Must be set to "server"
ShowMessageBox A Boolean value that specifies whether the summary should be displayed in a message box or not
ShowSummary A Boolean value that specifies whether the ValidationSummary control should be displayed or hidden

Example1 of ValidationSummary Control

Use the ValidationSummary control to write a bulleted list of fields that are required but left empty by the user.

C# Code

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class vale : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RequiredFieldValidator1.Text = "The name field is required!";
            RequiredFieldValidator1.ControlToValidate = TextBox1.ClientID;

            RangeValidator1.Text = "The date must be between 2015-02-05 and 2015-03-04!";
            RangeValidator1.MinimumValue = "2015-02-05";
            RangeValidator1.MaximumValue = "2015-03-04";
            RangeValidator1.ControlToValidate = TextBox2.ClientID;

            RegularExpressionValidator1.Text = "Email Address is Incorrect";
            RegularExpressionValidator1.ControlToValidate = TextBox3.ClientID;

        }
    }
}

HTML Code

<html>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:TextBox ID="TextBox1" runat="server" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="The name field is required!"></asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server">
        <asp:RangeValidator ID="RangeValidator1" runat="server" 
            ErrorMessage="The date must be between 2015-02-05 and 2015-03-04!"></asp:RangeValidator>
        <br />
        <br />
        <asp:TextBox ID="TextBox3" runat="server" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
            ErrorMessage="Email Address is Incorrect" 
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
        </asp:RegularExpressionValidator>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
    </div>
	</form>
</body>
</html>

Output




Example2 of ValidationSummary Control

Use the ValidationSummary control to display a message box of the fields that are required but left empty by the user.

C# Code

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class vale : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RequiredFieldValidator1.Text = "The name field is required!";
            RequiredFieldValidator1.ControlToValidate = TextBox1.ClientID;

            RangeValidator1.Text = "The date must be between 2015-02-05 and 2015-03-04!";
            RangeValidator1.MinimumValue = "2015-02-05";
            RangeValidator1.MaximumValue = "2015-03-04";
            RangeValidator1.ControlToValidate = TextBox2.ClientID;

            RegularExpressionValidator1.Text = "Email Address is Incorrect";
            RegularExpressionValidator1.ControlToValidate = TextBox3.ClientID;
            
            ValidationSummary1.ShowMessageBox = true;

        }
    }
}

HTML Code

<html>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:TextBox ID="TextBox1" runat="server" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="The name field is required!"></asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server">
        <asp:RangeValidator ID="RangeValidator1" runat="server" 
            ErrorMessage="The date must be between 2015-02-05 and 2015-03-04!"></asp:RangeValidator>
        <br />
        <br />
        <asp:TextBox ID="TextBox3" runat="server" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
            ErrorMessage="Email Address is Incorrect" 
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
        </asp:RegularExpressionValidator>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <br />
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="true" />
    </div>
	</form>
</body>
</html>

Output




Share this article on