1

Possible Duplicate:
textbox empty check using javascript

I have a asp.net button and a asp.net textbox, when I click on button, I want to check if textbox is empty or not but not sure how I can do that,

<div>
    <asp:TextBox ID="txtEU" runat="server"></asp:TextBox>
</div>
<div>
    <asp:ImageButton ID="button" runat="server" OnClientClick="MyFunction(); return false;" ImageUrl="/myfolder/abc.png" />
</div>

in my JavaScript I am doing,

   <script type="text/javascript">
    function doWork() 
    {  
        if($input[]

not sure how to check if its empty or not, if its empty then I am doing something if not then it should call a code behind method for that button.

Community
  • 1
  • 1
Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • @huMptyduMpty is that question telling how to call a code behind method as well ? – Mathematics Nov 23 '12 at 11:29
  • @huMptyduMpty This link you now posted asking me to have another button that will do a post back which I don't want in my case, I would suggest you to live in positive world with a positive mind you might get somewhere by chance :) – Mathematics Nov 23 '12 at 11:34

5 Answers5

13

Read on the ClientIDMode property to see how element ID are generated in ASP.NET (4.0 and above)

function doWork() 
{  
     var textbox = document.getElementById('<%=txtEU.ClientID%>');

     if(textbox.value.length == 0)
     {

     }
}

OR

if(textbox.value == "")

Using Validators will help you handle some of this validation out of the box. One of them is RequiredValidator, which evaluates the value of an input control to ensure that the user enters a value.

<asp:RequiredFieldValidator runat="server" ID="txtEURequiredValidator" ErrorMessage="EU should not be empty" />
ѺȐeallү
  • 2,887
  • 3
  • 22
  • 34
codingbiz
  • 26,179
  • 8
  • 59
  • 96
5

you have the ability to use a RequiredFieldValidator or a CustomValidator if you need to execute a more complex scenario.

Here is a good starting point i think: http://asp.net-tutorials.com/validation/introduction/ (check the links on the right side to have a detailed view of the validators)

Hope this helps.

Lionel D
  • 317
  • 1
  • 10
1

You can do like so:

if ($('#<%= txtEU.ClientID %>').val()({
   // String is not empty
}

Explanation:

  • Because, by default, asp.net mangles the html ID for the text box, you will need to inject the name into your jQuery.
  • In jQuery, null and empty can both be tested for with !
Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
1
 //javascript code
function Myfunction()
    {   
       if(document .getElementById("<%=txtEU.ClientID %>").value=="")
        {
            alert("Please Enter Text");
           txtEU.focus();
            return false;
        }

        return true;
    }
      //aspcode
 <asp:ImageButton ID="button" runat="server" OnClientClick="return Myfunction();" ImageUrl="/myfolder/abc.png" />
Atul Phadtare
  • 555
  • 3
  • 7
  • 13
0
if ($('#<%= yourtextboxname.ClientID %>').val() =="")
  // String is not empty
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Sharmaji TM
  • 199
  • 1
  • 13