There are six Web server controls that you can use to validate input on your Web Form pages. This article discusses the
ValidationSummary control.
The
ValidationSummary control is used to summarize the error messages from all validators on a Web page in a single location. The summary can be displayed as a list, a bulleted list, or as a single paragraph based on the
DisplayMode property. The summary can be displayed on the Web page and in a message box when you set the
ShowSummary and
ShowMessageBox properties accordingly.
The following sample code demonstrates uses of the
RequiredFieldValidator,
RangeValidator, and
ValidationSummary controls. If a value is not entered in the Name, Age, or Height text boxes, or if a
valid height is not entered in the Height text box, the
ValidationSummary control lists a summary of the validation errors.
NOTE: When you set the
ShowMessageBox value to
True on the
ValidationSummary control, the summary is displayed in a client-side message box.
<html>
<body>
<form runat=server id=form1>
<asp:Label ID="lblName" Runat=server>First Name:</asp:Label>
<asp:TextBox ID="txtName" Runat=server></asp:TextBox>
<asp:RequiredFieldValidator
id="RequiredFieldValidatorName"
ControlToValidate="txtName"
ErrorMessage="First Name field is empty"
Display="Static"
InitialValue="" Width="100%" runat=server>
</asp:RequiredFieldValidator><br>
<asp:Label ID="lblAge" Runat=server>Age:</asp:Label>
<asp:TextBox ID="txtAge" Runat=server></asp:TextBox>
<asp:RequiredFieldValidator
id="RequiredfieldvalidatorAge"
ControlToValidate="txtAge"
ErrorMessage="Age field is empty"
Display="Static"
InitialValue="" Width="100%" runat=server>
</asp:RequiredFieldValidator><br>
<asp:Label ID="lblHeight" Runat=server>Height:</asp:Label>
<asp:TextBox ID="txtHeight" Runat=server></asp:TextBox>
<asp:RequiredFieldValidator
id="RequiredfieldvalidatorHeight"
ControlToValidate="txtHeight"
ErrorMessage="Height field is empty"
Display="Static"
InitialValue="" Width="100%" runat=server>
</asp:RequiredFieldValidator>
<asp:RangeValidator
id="RangeValidatorHeight"
ControlToValidate="txtHeight"
ErrorMessage="Incorrect value for the height field"
Display="Static"
Type="Integer"
MinimumValue="1"
MaximumValue="10"
Width="100%" runat=server>
</asp:RangeValidator><br>
<asp:ValidationSummary
id="valSum"
DisplayMode="BulletList"
runat="server"
HeaderText="Summary of Validation Errors:"
Font-Name="verdana"
Font-Size="12"/><br>
<asp:Button id=btnValidate text="Validate" runat=server />
</form>
</body>
</html>
For additional information about Validation controls, browse to the following MSDN Web sites: