2

I have three textbox

<asp:textbox id="txt1" runat=server>
<asp:textbox id="txt2" runat=server>
<asp:textbox id="txt1" runat=server>
<asp:button ID="btnCheck" text="check" runat=server onclick="btnCheck_Click">

I want the txt1, and txt2 required if the txt1 has values.

Can this be possible in javascript/jquery because i dont want to trigger the onclick event of the checkbox.

  • The answer to your question is YES you can. What is the answer? maybe you can read this -> Check this http://stackoverflow.com/questions/13528117/how-to-check-if-a-asp-text-box-is-empty-or-not All what you have to do is learn how to 1)get the value of textbox 2)check if textbox is empty, 3)change the attribute of a textbox to enabled/disabled – Mohammed Joraid Jan 23 '14 at 05:43
  • Show what did you try, and why two controls have same Id? – Buzz Jan 23 '14 at 05:52

3 Answers3

1

You will need ClientID property to get element id . ClientID property gets the control ID for HTML markup that is generated by ASP.NET.

You won't be able to capture element if you simply use tetxbox Id value in javascript/jquery as when <asp:TextBox> tag is renedered by asp.net as HTML equivalent <input type="text> , asp.net also regenerates the id with system generated value.

<script type="text/javascript">
        $(function () {
            $('#<%=btnCheck.ClientID%>').click(function () {

                var txtBoxOne = $('#<%=txt1.ClientID%>').val();
                var txtBoxTwo = $('#<%=txt2.ClientID%>').val();
                var txtBoxThree = $('#<%=txt3.ClientID%>').val();
                if (txtBoxOne != "") {
                    if (txtBoxTwo == "" || txtBoxThree == "") {
                        alert('Please enter');
                        return;
                    }
                }
            });

        });

    </script>
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
  • This works great, now I have a second problem. When I click on the "OK" in the alert pop up, it will directly fire the button event. I want to prevent it going in the button event. – Registered_User012345 Jan 24 '14 at 00:33
0

Try this one

 function btnCheck_Click(){
       var txtbox1 = $('#yourtxt1id').val();
       if(txtbox1.value.length == 0){
       }
       else{
          $('#yourtxt3id').attr('disabled','true');

       }
  });
Dinesh
  • 1,005
  • 5
  • 16
  • 38
0

Firstly, you can not have duplicate id. It's should be unique, so try to change your HTML to:

<asp:textbox id="txt1" runat=server>
<asp:textbox id="txt2" runat=server>
<asp:textbox id="txt3" runat=server>
<asp:button ID="btnCheck" text="check" runat=server>

After that you can use this code to achieve your task:

$('#btnCheck').click(function() {
    if ($('#txt1').val().length > 0) {
        var txt2Val = $('#txt2').val().length,
            txt3Val = $('#txt3').val().length;            
        if( txt2Val == 0 || txt3Val == 0) {
            alert('Missing Value');   
        }
    }
});

Fiddle Demo

Felix
  • 37,892
  • 8
  • 43
  • 55