C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
ASP.NET ValidationIn this chapter, we will discuss about the data validation in the Web Forms. To perform validation, ASP.NET provides controls that automatically check user input and require no code. We can also create custom validation for our application. ASP.NET validation controlsFollowing are the validation controls
ASP.NET CompareValidator ControlThis validator evaluates the value of an input control against another input control on the basis of specified operator. We can use comparison operators like: less than, equal to, greater than etc. Note: If the input filed is empty, no validation will be performed.CompareValidator Properties
ExampleHere, in the following example, we are validating user input by using CompareValidator controller. Source code of the example is given below. // compare_validator_demo.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="compare_validator_demo.aspx.cs" Inherits="asp.netexample.compare_validator_demo" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .auto-style1 { width: 100%; } .auto-style2 { height: 26px; } .auto-style3 { height: 26px; width: 93px; } .auto-style4 { width: 93px; } </style> </head> <body> <form id="form1" runat="server"> <table class="auto-style1"> <tr> <td class="auto-style3"> First value</td> <td class="auto-style2"> <asp:TextBox ID="firstval" runat="server" required="true"></asp:TextBox> </td> </tr> <tr> <td class="auto-style4"> Second value</td> <td> <asp:TextBox ID="secondval" runat="server"></asp:TextBox> It should be greater than first value</td> </tr> <tr> <td class="auto-style4"></td> <td> <asp:Button ID="Button1" runat="server" Text="save"/> </td> </tr> </table> < asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="secondval" ControlToValidate="firstval" Display="Dynamic" ErrorMessage="Enter valid value" ForeColor="Red" Operator="LessThan" Type="Integer"></asp:CompareValidator> </form> </body> </html> Output:
Next TopicASP.NET RangeValidator
|