Skip to content Skip to sidebar Skip to footer

How To Check If A Asp Text Box Is Empty Or Not

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 no

Solution 1:

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

functiondoWork() 
{  
     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:RequiredFieldValidatorrunat="server"ID="txtEURequiredValidator"ErrorMessage="EU should not be empty" />

Solution 2:

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.

Solution 3:

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 !

Solution 4:

//javascript codefunctionMyfunction()
    {   
       if(document .getElementById("<%=txtEU.ClientID %>").value=="")
        {
            alert("Please Enter Text");
           txtEU.focus();
            returnfalse;
        }

        returntrue;
    }
      //aspcode
 <asp:ImageButtonID="button" runat="server"OnClientClick="return Myfunction();"ImageUrl="/myfolder/abc.png" />

Solution 5:

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

Post a Comment for "How To Check If A Asp Text Box Is Empty Or Not"