In this Asp.net validation control tutorial, you learn how to use different validation controls in asp.net application development.
Different type of Form validation controls to validate user input data
Namespace : System.Web.UI.WebControls
All validation controls are inherited from abstract class BaseValidator : Label, IValidator
Indicate the ID of the control to be validated.
Tells how the error message to be shown
Indicates if client side validation will occur
error message string
Indicates if the value of the control is valid
logical group of multiple validators
if validation fails then the error string to be displayed
If validation fails, then the focus should go to the related input control
The RequiredFieldValidator control mak sure that the required field is not empty, normally tied to a textbox control to force input into textbox.
<asp:TextBox ID="txtName" runat="server"> </asp:TextBox> <asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="Please enter Name" ControlToValidate="txtName"> </asp:RequiredFieldValidator>
Range Validator is used when we want to validate the value within two range, normally used for datatype like number, date etc.
Here in example we are checking the age range should be within 18 to 30
Syntax of RangeValidator
<asp:TextBox ID="txtAge" runat="server"> </asp:TextBox> <asp:RangeValidator ID="rvAge" runat="server"
ErrorMessage="Age should be 18 to 30" ControlToValidate="txtAge" type="Integer" MinimumValue="18" MaximumValue="30">
</asp:RangeValidator>
Look at the property value of Type, MinimumValue & MaximumValue
CompareValidator control is used to compare a value with value of another control or a fixed value.
Here in example below we are comparing password and re-password is equal
Password <asp:TextBox ID="txtPass" runat="server"> </asp:TextBox> Confirm <asp:TextBox ID="txtPassRe" runat="server"> </asp:TextBox> <asp:CompareValidator ID="cvPass" runat="server" ErrorMessage="Password should match" ControlToValidate="txtPass" ControlToCompare="txtPassRe" Operator="Equal"> </asp:CompareValidator>
Id of main control textBox
Id of control to compare with
if you want to compare with any fixed value
Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual
RegularExpressionValidator is used for validating the user input matching against a pattern of a regular expression
In example below we are checking if user has entered a valid email
<asp:TextBox ID="txtEmail" runat="server"> </asp:TextBox> <asp:RegularExpressionValidator ID="revEmail" runat="server" ErrorMessage="Invalid Email" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator>
ValidationExpression
is string property that contain the expression, and check if user input is matching the expression, if not then display the ErrorMessage
The CustomValidator control allows writing any type of validation logic in your .net code then calling that method in CustomValidator
<asp:TextBox ID="txtCustom" runat="server" ></asp:TextBox> <asp:CustomValidator ID="cv" runat="server" ControlToValidate="txtCustom" ErrorMessage="Custom Validator Error" OnServerValidate="cv_ServerValidate"> </asp:CustomValidator>//here is the server side method
protected void cv_ServerValidate(object source,
ServerValidateEventArgs args) { if (args.Value == "webtrainingroom") args.IsValid = true; else args.IsValid = false; }
ValidationSummary control is for displaying all validation error in one place, simply place the ValidationSummary control in your form where you want all message to be displayed
<asp:ValidationSummary ID="vsForm" runat="server" DisplayMode = "BulletList" ShowSummary = "true" />
You may also read how validation in Asp.Net MVC works.