lunes, 10 de diciembre de 2012

Validating values between TextBoxes using Javascript

<head runat="server">
    <title></title>
 
    <script type="text/javascript" language="javascript">
        function leave()
        {
            try
            {
                var val = document.getElementById('TextBox2').value;
                var compara = document.getElementById('TextBox1').value;
 
                if ((val > 0) && (compara > 0) && (compara > (val * 0.5)))
                {
                    document.getElementById('Label1').innerHTML = 'ERROR';
                    document.getElementById('Label2').innerHTML = 'ERROR';
                }
                else
                {
                    document.getElementById('Label1').innerHTML = '';
                    document.getElementById('Label2').innerHTML = '';
                }
            }
            catch (err)
            {
                //
            }
        }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"  OnBlur="leave();" />
        <asp:Label ID="Label2" runat="server" Text="Label" />
        <br />
        <asp:TextBox ID="TextBox2" runat="server" OnBlur="leave();" />
        <asp:Label ID="Label1" runat="server" Text="Label" />
    </div>
    </form>
</body>
</html>