Click here to Skip to main content
15,924,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

I have five text boxes and need to validate values in each text box based on previous textboxes, like .., textbox3 should be greaer than textbox2 ,if textbox2 is null then textbox3 should be greater than textbox1. Is there any way to do this in javascript or jquery, . Please guide thru this logic,,
thanks in advance.
Posted
Updated 5-May-17 2:49am
Comments
Mohibur Rashid 24-Aug-13 11:35am    
there is way to do it, but did you try anything at all?

1 solution

Below JavaScript sample code may help.
<html>
<head>
    <title></title>
</head>
<body>
    <input type="text" id="textbox1" />
    <br />
    
    <br />
    <input type="text" id="textbox3" />
    <br />
    <input type="text" id="textbox4" />
    <input type="button" value="Validate" onclick="ValidateTextBox()" />
    <script>
    function ValidateTextBox() {
        var lastTextBox = null;
        var textBoxCount =0;
        for (var i = 2; i <= 4; i++) {
            lastTextBoxid = "textbox" + (i - 1);
            if (document.getElementById(lastTextBoxid) != null) {
                lastTextBox = document.getElementById(lastTextBoxid);
                textBoxCount++;
            }
            if (document.getElementById("textbox" + i) != null) {
                var thisTextBox = document.getElementById("textbox" + i);
                if (parseFloat(lastTextBox.value) > parseFloat(thisTextBox.value)) {
                alert("Text Box " + (textBoxCount+1) + " value should be grater than TextBox" + (textBoxCount));
                return false;
                }
            }

        }


    }

    </script>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900