Validate TextBox value before Button Click using ASP.Net CustomValidator in JavaScript

indradeo
 
on Jul 22, 2022 10:58 PM
529 Views

Please look my c# code.

How to add validtion of Grand Total () amount not lessthen rs: 5 and not greater then rs: 5 form Pa

Validation come after click on calculate button.

Only this validation pending in my project.

Thanks for all time supports.

Please help me to do this.

private void calcualtion1()
{
    Double num1 = Double.Parse(TextBox1.Text);
    Double num2 = Double.Parse(TextBox15.Text);
    Double sum = num1 + num2;
    TextBox16.Text = sum.ToString();
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Jul 23, 2022 02:49 AM

Hi indradeo,

Please refer below sample.

HTML

First Value:<asp:TextBox ID="txtFirstValue" runat="server" onchange="Calculate()" /><br />
Second Value:<asp:TextBox ID="txtSecondValue" runat="server" onchange="Calculate()" /><br />
Grand Total:<asp:TextBox ID="txtGrand_Total" runat="server" /><br />
Parking Amount:<asp:TextBox ID="txtParkingAmount" runat="server" />
<br />
<asp:CustomValidator ID="cvValidate" runat="server" ClientValidationFunction="ValidateNumber" ForeColor="Red"
    ErrorMessage="Input Number is not valid" ControlToValidate="txtParkingAmount"></asp:CustomValidator>
<script type="text/javascript">
    function Calculate() {
        var firstValue = document.getElementById("txtFirstValue");
        var secondValue = document.getElementById("txtSecondValue");

        if (firstValue.value == "" || isNaN(firstValue.value)) {
            firstValue.value = 0;
        }
        if (secondValue.value == "" || isNaN(secondValue.value)) {
            secondValue.value = 0;
        }

        var total = parseInt(firstValue.value) + parseInt(secondValue.value);
        document.getElementById("txtGrand_Total").value = total;
    }

    function ValidateNumber(sender, args) {
        var firstValue = document.getElementById("txtFirstValue").value;
        var secondValue = document.getElementById("txtSecondValue").value;
        var number = document.getElementById(sender.controltovalidate).value;
        var rangeFrom = (parseInt(firstValue) + parseInt(secondValue)) - 5;
        var rangeTo = (parseInt(firstValue) + parseInt(secondValue)) + 5;
        if (number >= rangeFrom && number <= rangeTo) {
            args.IsValid = true;
        } else {
            args.IsValid = false;
        }
    }
</script>

Screenshot