Compare validator to compare two numbers in ASP.Net

indradeo
 
on Jul 15, 2022 03:22 AM
402 Views

Dear Sir,

I want to add validation Parking_Amount(₹) Less than or equal to SRV_Amount(₹).

<body align="center" style="background-color: #FFFFFF; text-align: left;">
    <form id="form1" runat="server">
    <table width="100%" height="30" border="1" cellspacing="1">
        <tr>
           <td class="auto-style27"><span class="auto-style28"></span><img src="images/m1.png" class="auto-style1"/><span class="auto-style28"> </span> </td>
        <th colspan="13" class="auto-style60" style="box-sizing: border-box;" ><span class="auto-style38"><span class="auto-style39">Meja Thermal Power Plant</span><br class="auto-style39" />
            </span><span class="auto-style37">

            <font style="box-sizing: border-box;">
                <span class="auto-style36">Bill Processing Monitoring System</span></font></th>
        <tr>
            <td class="auto-style24"><strong>Date:<br />
                </strong></span> <asp:Label ID="Label16" runat="server" Text="" CssClass="auto-style42"></asp:Label></td>
            <td class="auto-style51" colspan="3">Emp. Name :
                <asp:Label ID="Label2" runat="server" CssClass="auto-style42" Text=""></asp:Label>
                &nbsp;,&nbsp;&nbsp;&nbsp;
                Emp. ID :&nbsp;&nbsp;<asp:Label ID="Label1" runat="server" Text="" CssClass="auto-style42"></asp:Label>
            &nbsp;
                 Department :&nbsp;&nbsp;<asp:Label ID="Label3" runat="server" Text="" CssClass="auto-style42"></asp:Label>
            &nbsp;
            </td>
            <td class="auto-style51" colspan="3">
                <asp:Button ID="Button2" runat="server" Height="25px" Text="PO Status" Width="122px" CssClass="auto-style48" OnClick="Button5_Click" CausesValidation="false"/>
            </td>
            <td class="auto-style51" colspan="2">
                <asp:Button ID="Button3" runat="server" Height="25px" Text="Change Password" Width="122px" CssClass="auto-style48" OnClick="Button3_Click" CausesValidation="false"/>
            </td>
            <td class="auto-style51" colspan="5">
                <asp:Button ID="Button4" runat="server" Height="25px" Text="Logout" Width="122px" CssClass="auto-style49" OnClick="Button4_Click" CausesValidation="false" />
            </td>
        </tr>
        <tr>
            <td class="auto-style64">SAP PO No. </td>
            <td class="auto-style65">
                <asp:TextBox ID="TextBox1" runat="server" Height="29px" Width="220px" CssClass="auto-style37" ></asp:TextBox>
                 <span class="auto-style37">
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="TextBox1" ErrorMessage="!!" ForeColor="Yellow" 
                        SetFocusOnError="True"></asp:RequiredFieldValidator> <br />
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"  ForeColor="Yellow"
ControlToValidate="TextBox1" ErrorMessage="Must be 10-Digit Numeric."  
ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator> </span> </td>
            <td class="auto-style67">Agency</td>
            <td class="auto-style61">
                <asp:TextBox ID="TextBox2" runat="server"  Height="29px" Width="220px" CssClass="auto-style37" ></asp:TextBox>
                <span class="auto-style37">
             <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="!!" ForeColor="Yellow" SetFocusOnError="True"></asp:RequiredFieldValidator>  <br />  
                &nbsp
                </span>    
            </td>
        </tr>
        <tr>
            <td class="auto-style71">SRV Number </td>
            <td class="auto-style72" >

                <asp:TextBox ID="TextBox3" runat="server" Height="29px" Width="220px" CssClass="auto-style37" ></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="TextBox3" ErrorMessage="!!" ForeColor="Yellow" SetFocusOnError="True"></asp:RequiredFieldValidator>  <br />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="TextBox3" runat="server" ErrorMessage="Only Numbers allowed" ForeColor="Yellow" ValidationExpression="\d+"></asp:RegularExpressionValidator>
            </td>
            <td class="auto-style73">SRV Date</td>
            <td class="auto-style58">
           
                                <asp:TextBox ID="TextBox4" runat="server"  Height="29px" Width="220px" CssClass="auto-style37" ></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox4" ErrorMessage="!!" ForeColor="Yellow" SetFocusOnError="True"></asp:RequiredFieldValidator>  
                <asp:CompareValidator id="dateValidator" runat="server"  Type="Date" Operator="DataTypeCheck" ControlToValidate="TextBox4"  ErrorMessage="Please enter a valid date." ForeColor="Yellow" ></asp:CompareValidator>
             </td>
            <td class="auto-style74" colspan="2">SRV_<strong>Amount_(₹)</strong></td>
            <td class="auto-style58" colspan="6">
            <asp:TextBox ID="TextBox5" runat="server" Height="26px" Width="215px"  CssClass="auto-style37" ></asp:TextBox>
              <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox5" ErrorMessage="!!" ForeColor="Yellow" SetFocusOnError="True"></asp:RequiredFieldValidator><br />
                  <asp:RegularExpressionValidator ID="RegularExpressionValidator4" ControlToValidate="TextBox5" runat="server" ErrorMessage="Only Numbers allowed" ForeColor="Yellow" ValidationExpression="\d+"></asp:RegularExpressionValidator> 
            </td>
        </tr>
        <tr>
            <td class="auto-style104">Invoice No. Agenst SRV</td>
            <td class="auto-style105" colspan="11"><asp:TextBox ID="TextBox11" runat="server"  Height="29px" Width="1110px" CssClass="auto-style37" ></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" 
                        ControlToValidate="TextBox11" ErrorMessage="!!" ForeColor="Yellow" 
                        SetFocusOnError="True" CssClass="auto-style37"></asp:RequiredFieldValidator>
               
            </td>
            <td class="auto-style106" colspan="2"></td>
        </tr>
        <tr>
            <td class="auto-style98">Parking <span style="color: rgb(255, 255, 255); font-family: &quot;Times New Roman&quot;; font-size: medium; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 700; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(0, 102, 102); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">Number </span></td>
            <td class="auto-style99">
                       <asp:TextBox ID="TextBox7" runat="server"  Height="26px" Width="220px" CssClass="auto-style37"></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                        ControlToValidate="TextBox7" ErrorMessage="!!" ForeColor="Yellow" 
                        SetFocusOnError="True"></asp:RequiredFieldValidator> <br /> 
                <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"  ForeColor="Yellow"
ControlToValidate="TextBox7" ErrorMessage="Must be 10-Digit Numeric."  
ValidationExpression="[0-9]{10}" style="font-size: medium"></asp:RegularExpressionValidator>  
              </td>
            <td class="auto-style100" >
                <strong>Parking Date</strong></td>
            <td class="auto-style101" >
                <strong>
                <asp:TextBox ID="TextBox8" runat="server"  Height="29px" Width="220px" CssClass="auto-style37" ></asp:TextBox>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
                        ControlToValidate="TextBox8" ErrorMessage="!!" ForeColor="Yellow" 
                        SetFocusOnError="True"></asp:RequiredFieldValidator><br />
                <asp:CompareValidator ID="CompareValidator4" runat="server"
                    ControlToCompare="TextBox4" ControlToValidate="TextBox8"
                    CultureInvariantValues="True" ValidationGroup="Date" ForeColor="Yellow"
                    ErrorMessage="Greater than SRV  date."
                    Operator="GreaterThan" SetFocusOnError="True" style="font-size: medium"></asp:CompareValidator>
                 </strong></td>
            <td class="auto-style102" >
                Parking_Amount  (₹)  </td>
            <td class="auto-style102" colspan="6" > 
                <asp:TextBox ID="TextBox6" runat="server"  Height="29px" Width="220px" CssClass="auto-style37" ></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                        ControlToValidate="TextBox6" ErrorMessage="!!" ForeColor="Yellow" 
                        SetFocusOnError="True" CssClass="auto-style37"></asp:RequiredFieldValidator><br />
                               <asp:CompareValidator ID="CompareValidator3" runat="server" operator="LessThanEqual"    
    ControlToCompare="TextBox5" ControlToValidate="TextBox6"   ForeColor="Yellow" Font-Size="Medium"
    ErrorMessage="Less than equal to SRV Amount."></asp:CompareValidator>
                </td>
            <td colspan="3" class="auto-style103" >
                &nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style96" >Email.Id</td>
            <td class="auto-style58" >
              
                <asp:TextBox ID="TextBox10" runat="server"  Height="29px" Width="220px" CssClass="auto-style37" ></asp:TextBox>
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" 
                        ControlToValidate="TextBox10" ErrorMessage="!!" ForeColor="Yellow" 
                        SetFocusOnError="True" CssClass="auto-style37"></asp:RequiredFieldValidator>
             </td>
            <td class="auto-style93">
                Phone No.</td>
            <td colspan="7" class="auto-style58">
                <asp:TextBox ID="TextBox9" runat="server"  text="91" Height="29px" Width="220px" CssClass="auto-style37" ></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                        ControlToValidate="TextBox9" ErrorMessage="!!" ForeColor="Yellow" 
                        SetFocusOnError="True" CssClass="auto-style37"></asp:RequiredFieldValidator>
            </td>
            <td colspan="4" class="auto-style95">
                </td>
        </tr>
        <tr>
            <td class="auto-style59"></td>
            <td class="auto-style44" colspan="12" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="Button1" runat="server" CssClass="auto-style13" Height="39px" Text="Save" Width="115px" OnClick="Button1_Click" />
                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="Button5" runat="server" CssClass="auto-style13" Height="39px" Text="Cancel" Width="115px" OnClick="Button2_Click" CausesValidation="false" />
            </td>
            <td class="auto-style34"></td>
        </tr>
    </table>
    </form>
</body>
</html>

 

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Jul 15, 2022 05:16 AM
on Jul 15, 2022 05:27 AM

Hi indradeo,

You need to set CompareValidator Operator property with LessThanEqual and the ControlToValidate and ControlToCompare.

Please refer below sample.

HTML

Parking_Amount: 
<asp:TextBox ID="txtParking_Amount" runat="server"></asp:TextBox>
<br />
SRV_Amount :
<asp:TextBox ID="txtSRV_Amount" runat="server"></asp:TextBox>
<br />
<asp:CompareValidator ID="cvAmounts" runat="server" ControlToValidate="txtParking_Amount" Operator="LessThanEqual"
    ControlToCompare="txtSRV_Amount" ValidationGroup="Amount" ForeColor="Red" Type="Integer"
    ErrorMessage="Parking_Amount must be less then SRV_Amount."></asp:CompareValidator>
<br />
<br />
<asp:Button ID="btnCompare" runat="server" Text="Comapre" ValidationGroup="Amount" />

Screenshot