Validate TextBox based on another TextBox value using ASP.Net CustomValidator

indradeo
 
on Jul 20, 2022 04:19 AM
Sample_204910.zip
769 Views

Dear Sir,

How to add validation of Grand Total (₹) amount not less than rs: 5 and not greater then rs: 5 form Parking Amount(₹)

Parking Amount(₹) : Label9.Text Grand Total (₹) : Label18.Text

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Jul 22, 2022 01:07 AM

Hi indradeo,

You need to use CustomValidator ClientValidationFunction property.

Please refer below sample.

HTML

Grand Total: <asp:TextBox ID="txtGrand_Total" runat="server" /><br />
Parking Amount: <asp:TextBox ID="txtParkingAmount" runat="server" /><br />
<asp:CustomValidator runat="server" ClientValidationFunction="ValidateNumber" ForeColor="Red"
    ControlToValidate="txtParkingAmount" ErrorMessage="Input Number is not valid." />
<script type="text/javascript">
    function ValidateNumber(sender, args) {
        var total = document.getElementById("txtGrand_Total").value;
        var number = document.getElementById(sender.controltovalidate).value;
        var rangeFrom = parseInt(total) - 5;
        var rangeTo = parseInt(total) + 5;

        if (number >= rangeFrom && number <= rangeTo) {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    }
</script>

Screenshot