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.