ASP.Net Input Validation Controls
In the tutorial, you will learn how to use input validation controls In ASP.Net like RequiredFieldValidator, CompareValidator, RegularExpressionValidator, CustomValidator, ValidationSummary, and RangeValidator.
Definition of ASP.Net : ASP.NET is a server side web application framework built by Microsoft and designed to create dynamic Web pages. This framework was introduced back to January 2002 with the initial release version 1.0 of .Net Framework. It is the next generation version of Active Server Pages known as ASP.
1. RequiredFieldValidator Control In ASP.Net
RequiredFieldValidator control is used to validate and check if an input is empty.
//Textbox to accept any input value <asp:TextBox runat="server" ID="txtName" /> //required validator control to validate textbox input <asp:RequiredFieldValidator ID="reqMyName" ControlToValidate="txtName" runat="server" ErrorMessage="Your name is required" Display="dynamic" />
2. CompareValidator Control In ASP.Net
CompareValidator control is used to compare a value of one control to the other value in another control or other fixed value.
/Compare to Password field <asp:TextBox runat="server" ID="txtPassword" TextMode="Password" /> //Compare Confirm Password field <asp:TextBox runat="server" iD="txtConfirmPassword" TextMode="Password" /> //Compare Validator <asp:CompareValidator ID="reqCompareValidator" runat="server" ControlToValidate="txtPassword" ControlToCompare="txtConfirmPassword" ErrorMessage="Both passwords have to be match." Display="dynamic" /> //Compare between one control to other fixed value <asp:TextBox runat="server" ID="txtYearToJoin" /> <asp:CompareValidator ID="reqYearToJoin" runat="server" ControlToValidate="Age" ValueToCompare="17" Display="dynamic" ErrorMessage="Minimum requirement to join is 17 years old" Type="Integer" Operator="GreaterThanEqual" /> //Available operators types Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, DataTypeCheck
3. RegularExpressionValidator Control In ASP.Net
RegularExpressionValidator control is used to validate a control value by whether the control value is matched with a given regular expression pattern.
//Textbox to accept numbers only <asp:TextBox ID="txtNumber" runat="server" /> //RegularExpressionValidation Control <asp:RegularExpressionValidator ID="regexNumber" runat="server" ControlToValidate="txtNumber" ValidationExpression="^[0-9] $" ErrorMessage="Invalid number" Display="Dynamic"/>
4. ASP.Net RangeValidator Control
RangeValidator control is used to validate if a control value matches with the predefined range value. Those predefined range values are MinimumValue, MaximumValue, and Type. There are 5 types that you can define, which will be Currency, Date, Double, Integer, and String.
//Textbox to accept numbers only between predefined range <asp:TextBox runat="server" ID="txtNumber" /> //RangeValidator Control <asp:RangeValidator ID="reqRangeValidator" runat="server" Display="dynamic" ControlToValidate="txtNumber" Type="Integer" ErrorMessage="The number must be between 100-200" MinimumValue="100" MaximumValue="200" />
5. CustomValidator Control In ASP.Net
CustomValidator Control is used when you have a custom javascript validation or server behind code validation.
//Example of using javascript validation that will check if input entered falls between 100-200 integer value <script type="text/javascript"> function CheckNumberRange(source, arguments) { if (arguments.Value >= 100 & arguments.Value <= 200) { arguments.IsValid = true; } else { arguments.IsValid = false; } } </script> //Textbox field to accept number of integer range between 100 to 200 <asp:TextBox id="txtNumberOfRange" runat="server" /> //CustomValidator range
Another example that will use ServerValidation
//Textbox field to accept number of integer range between 100 to 200 <asp:TextBox id="txtNumberOfRange" runat="server" /> //Button to check the range value <asp:Button id="btnCheckRange" Text="Validate" runat="server"/> //Label to show the result <asp:Label id="lblResult" runat="server" /> //CustomValidator range <asp:CustomValidator id="reqCustomValidator" ControlToValidate="txtNumberOfRange" OnServerValidate="CheckNumberRange" Display="Dynamic" ErrorMessage="Invalid range. Must be between 100-200" runat="server"/> //Button validator to check and return the page validation void btnCheckNumber_OnClick(object sender, EventArgs e){ if(Page.IsValid){ lblResult.Text = "Page is valid."; }else{ lblResult.Text = "Page is not valid!"; } } //Function Server Validator void CheckNumberRange(object source, ServerValidateEventArgs arguments) { int num = 0; arguments.IsValid = false; if (int.TryParse(arguments.Value, out num)) { num = int.Parse(arguments.Value); if(num >= 100 & 200 >= num){ arguments.IsValid = true; } } }
6. ASP.Net ValidationSummary Control
ValidationSummary Control is used to show the list or summary of all available validator errors. It does not perform any validation.
//ValidationSummary Control <asp:ValidationSummary ID="reqValidationSummary" runat="server" ShowSummary="true" ShowMessageBox="true" DisplayMode="BulletList" HeaderText="Please see the following errors:"/>
Variable Properties:
- ShowMessageBox: true or false. If set to true, it will display a message box.
- ShowSummary: true or false. If set to true, it will summary of the errors text in the page.
- DisplayMode: BulletList, List, and SingleParagraph. The style of the errors list text to be displayed.