Focus to last TextBox if Input value is Zero or Null using jQuery in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

I have some textbox in asp.net. And if the user enters the 0 or null value in fields, before inserting them to DB  the pointer jumps to the last textbox and disable another textbox. also, I want to do this action in HTML code. how can I do this?!

  <form id="form1" runat="server">
        <div>
        </div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        <br />
        <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>
    </form>

 

You are viewing reply posted by: dharmendr one month ago.
Posted one month ago Modified on 29 days ago

Hi dorsa,

Check this example. Now please take its reference and correct your code.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        var current;
        $('.Validate').on('keyup', function (e) {
            current = $(this);
            if (current.val() == 0 || current.val() == 'null') {
                $(".Validate").each(function (i, item) {
                    if ($(current)[0].id != $(item)[0].id) {
                        if ($(item).val() == '') {
                            $(item).attr('disabled', 'disabled');
                        }
                    }
                });
                if (current.val() != '') {
                    $('[id*=TextBox8]').focus();
                }
            } else {
                $(".Validate").each(function (i, item) {
                    $(item).removeAttr('disabled');
                });
            }
        });
    });
</script>
<div>
</div>
<asp:TextBox ID="TextBox1" runat="server" CssClass="Validate"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server" CssClass="Validate"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox3" runat="server" CssClass="Validate"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox4" runat="server" CssClass="Validate"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox5" runat="server" CssClass="Validate"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox6" runat="server" CssClass="Validate"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox7" runat="server" CssClass="Validate"></asp:TextBox>
<br />
<br />
<br />
<br />
<asp:TextBox ID="TextBox8" runat="server"></asp:TextBox>

Screenshot