Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Making a field required

The simplest validation rule is that a field must not be empty. A required field validator enforces it for you.

Attach a validator to a control

Markup
<asp:TextBox id="txtEmail" runat="server" />
<asp:RequiredFieldValidator
    ControlToValidate="txtEmail"
    ErrorMessage="Please enter your email"
    runat="server" />

What it checks

  • Whether the target control has any content at all.
  • By default, whitespace counts as empty, which is usually what you want.
  • The message appears near the field when the check fails.
Validation is a courtesy, not a guarantee. It improves the experience for honest visitors. Your server code must still check everything, because validation can be bypassed. This pairs with reading what the visitor typed.

For a value that must sit inside a range, use the range validator.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.