![toolbox]()
Validation Controls in ASP.NET is very interesting topic, the reason is by using these controls you can restrict users from entering invalid content.
ASP.NET validation controls provide two ways of validation: Server-side or Client-side. So as per our requirement we can choose either or both of them.
There are six validation controls available in ASP.NET Toolkit by default. If you want to use some other fancy validation controls, then you can download AJAX toolkit from CodePlexwebsite.
- Required Field Validator
- Compare Validator
- Range Validator
- Regular Expression Validator
- Custom Validator
- Validation Summary
Details with Syntax:
Required Field Validator: Checks whether control contains data.
- <asp:RequiredFieldValidator ID="UrlPathRequired"
- runat="server" ControlToValidate="UrlPath"
- ErrorMessage="Please check URL">*</asp:RequiredFieldValidator>
Compare Validator: Comparison between two controls data.
- <asp:CompareValidator ID="CompareValidatorPassword" runat="server"
- ErrorMessage="Password do not match">
- </asp:CompareValidator>
Range Validator: Checks if the data to be validated falls between the specified ranges.
- <asp:RangeValidator ID="RangeValidatorAge" runat="server"
- ControlToValidate="txtAge" ErrorMessage="Enter your age (18 - 25)"
- MaximumValue="25" MinimumValue="18" Type="Integer">
- </asp:RangeValidator>
Regular Expression Validator: Email Format,Phone Format etc.
- <asp:RegularExpressionValidator ID="URLPathExpressionValidator" runat="server"
- ControlToValidate="UrlPath" ErrorMessage="Invalid URL Path"
- ValidationExpression="string" ValidationGroup="string">Invalid URL
- </asp:RegularExpressionValidator>
Custom Validator: Checks the validity of an entered items through a client side script or server side code.
- <asp:CustomValidator ID="URLPathCustomValidator" runat="server"
- ErrorMessage="URL Path exists" ControlToValidate="UrlPath"
- OnServerValidate="UrlPathCustomValidator_ServerValidate">
- URL is already used by another application or folder
- </asp:CustomValidator>
Validation Summary: It doesn’t performs any validations. However, displays a summary of validation errors.
- <asp:ValidationSummary ID="ValidationSummaryForm" runat="server"
- DisplayMode = "BulletList" ShowSummary = "true"
- HeaderText="Form contains below Errors:" />
Hope you found this post useful and worth. Keep watching this space & stay connected for more stuffs on ASP.NET. For more such articles, please follow my
blog.
What do you think?
Dear Reader,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.
Happy Learning.