Validating User Input with Validation Controls
ASP.NET Web Forms provides a family of validator server controls — RequiredFieldValidator, RangeValidator, RegularExpressionValidator, CompareValidator, CustomValidator, and ValidationSummary — that attach to an input control via ControlToValidate and automatically render both client-side JavaScript checks and server-side re-validation before Page_Load logic runs. Each validator sets Page.IsValid to false when its condition fails, letting code-behind check a single flag rather than manually inspecting every field.
Cricket analogy: A RequiredFieldValidator is like the third umpire refusing to confirm a run-out review until the on-field umpire submits a decision, blocking the process until the required input is present.
Built-in Validator Controls
RequiredFieldValidator ensures a field isn't left at its InitialValue (typically empty), RangeValidator checks that a numeric, date, or currency value falls between MinimumValue and MaximumValue, RegularExpressionValidator matches input against a ValidationExpression pattern such as an email or phone format, and CompareValidator either compares two controls' values (e.g., password confirmation) or validates a single value's data type. Each validator control also renders an ErrorMessage that ValidationSummary can collect and display in one place, typically at the top of the form.
Cricket analogy: RegularExpressionValidator checking a jersey number format is like the scorer's software rejecting '7a' as an invalid over count, insisting the entry matches a strict numeric pattern like a legal delivery count.
<asp:TextBox ID="txtEmail" runat="server" />
<asp:RequiredFieldValidator ID="rfvEmail" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Email is required." Display="Dynamic" />
<asp:RegularExpressionValidator ID="revEmail" runat="server"
ControlToValidate="txtEmail" ErrorMessage="Enter a valid email address."
ValidationExpression="^[^@\s]+@[^@\s]+\.[^@\s]+$" Display="Dynamic" />
<asp:TextBox ID="txtAge" runat="server" />
<asp:RangeValidator ID="rvAge" runat="server" ControlToValidate="txtAge"
Type="Integer" MinimumValue="18" MaximumValue="99"
ErrorMessage="Age must be between 18 and 99." />
<asp:ValidationSummary ID="valSummary" runat="server" HeaderText="Please fix the following:" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<script runat="server">
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
SaveRegistration(txtEmail.Text, int.Parse(txtAge.Text));
}
}
</script>CustomValidator and Client vs. Server Validation
When built-in validators can't express a business rule, CustomValidator lets you supply both a server-side OnServerValidate handler and, optionally, a ClientValidationFunction written in JavaScript, giving identical logic on both tiers. Validation runs client-side first for instant feedback via the WebForms validation JavaScript library, but the server-side check is not optional: because client script can be disabled or bypassed entirely, every validator also re-runs on the server, and Page.IsValid must always be checked in code-behind before touching the database, regardless of what the client reported.
Cricket analogy: Relying only on client-side validation is like trusting a fan's phone app for the DRS decision instead of the official third umpire, since the phone app can be spoofed while the official review cannot.
Never rely on client-side validation alone for security or data integrity. A user can disable JavaScript or submit a crafted POST request directly, bypassing all client checks entirely. Always check Page.IsValid in the server-side event handler before processing input.
- Validator controls attach to an input via ControlToValidate and set Page.IsValid when their condition fails.
- RequiredFieldValidator, RangeValidator, RegularExpressionValidator, and CompareValidator cover the most common built-in rules.
- ValidationSummary aggregates every validator's ErrorMessage into a single summary display.
- CustomValidator supports both a server-side OnServerValidate handler and an optional client-side ClientValidationFunction.
- Client-side validation improves user experience but can be bypassed, so server-side re-validation always occurs automatically.
- Page.IsValid must be checked explicitly in code-behind before performing any data-modifying operation.
- Display="Dynamic" vs "Static" controls whether space for the error message is reserved even when no error is shown.
Practice what you learned
1. Which property must every validator control set to link it to the input it checks?
2. What is the single most important flag to check in code-behind before processing form data on a postback?
3. Which validator control is designed to implement custom business logic that no built-in validator covers?
4. Why can't client-side validation alone be trusted for security?
5. Which control aggregates the ErrorMessage text of every failed validator on a page into one display area?
Was this page helpful?
You May Also Like
Data Controls: GridView and Repeater
Learn how ASP.NET Web Forms renders bound data using the feature-rich GridView control and the fully customizable Repeater control.
Forms and Validation in Web Pages
Learn how ASP.NET Web Pages reads posted form data and enforces validation rules using the Validation helper, from server checks to optional client-side hints.
The Postback Model
How ASP.NET Web Forms simulates a stateful, event-driven experience over stateless HTTP through postbacks, __doPostBack, and cross-page posting.
Server Controls
An explanation of ASP.NET Web Forms server controls — HTML server controls, Web server controls, and data-bound controls like GridView — and how they raise events.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics