Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
2.60/5 (5 votes)
See more:
does anybody have any idea about how to check whether a text box is empty ?
Posted
Updated 2-May-18 21:12pm

Use this code
C#
if(!string.IsNullOrEmpty(tb.Text))// Note. Tb is an instance of a TextBox in your application
{   
  // Do something
}
 
Share this answer
 
Comments
Nish Nishant 29-Apr-11 22:42pm    
My vote of 5.
Pritesh Aryan 30-Apr-11 3:12am    
good answer
Kim Togo 30-Apr-11 8:22am    
It will fail, if you fill the TextBox with whitespace.
Ed Nutting 30-Apr-11 8:26am    
My 5. Good answer though you should probably combine your answer with Kim Togo's to form an if statement like:

if(string.IsNullOrEmpty(this.textBox1.Text) || string.IsNullOrWhiteSpace(this.textBox1.Text))
Wonde Tadesse 6-Dec-11 10:28am    
I thought about string.IsNullOrWhiteSpace(this.textBox1.Text) method, White space is a character and hence I found it to difficult to say empty.
Use String.IsNullOrWhiteSpace[^] method instead of String.IsNullOrEmpty().

String.IsNullOrWhiteSpace() is a combination of String.IsNullOrEmpty() and String.Trim().Length == 0

The String.Trim()[^] removes all leading and trailing white-space characters

if(string.IsNullOrWhiteSpace(this.textBox1.Text))
{
  MessageBox.Show("TextBox is empty");
}
 
Share this answer
 
v2
Comments
Ed Nutting 30-Apr-11 8:27am    
My 5. Good answer though you should probably combine your answer with Wonde Tadesse's to form an if statement like:

if(string.IsNullOrEmpty(this.textBox1.Text) || string.IsNullOrWhiteSpace(this.textBox1.Text))
Anand Dhamane 12-Oct-13 7:41am    
Thanks.It is Good.
Kim Togo 30-Apr-11 8:31am    
Thanks EdMan. :-)

No need to make that if statement.

String.IsNullOrWhiteSpace is a combination of String.IsNullOrEmpty and String.Trim functions.
Ed Nutting 30-Apr-11 8:37am    
Fair enough. Might want to improve your question to make that a little clearer ;P
Kim Togo 30-Apr-11 8:49am    
Solution has been updated. Thanks EdMan.
Hi anyavacy,

You can also use this code:
if(textBox1.Text.Equals(""))
{
    // write the code you want
}


I hope this helps,
Good luck,
:)
 
Share this answer
 
Comments
Akinmade Bond 9-Oct-12 8:27am    
But that would only check if the TextBox is empty.
Michael Waguih 26-Jan-13 14:05pm    
and that was the question
Akinmade Bond 27-Jan-13 10:58am    
True. ;)
You can use required="" property

C#
<asp:TextBox ID="txtName" runat="server" Width="165px" required=""></asp:TextBox>


It work fine on button click
 
Share this answer
 
Comments
CHill60 3-May-18 3:52am    
Given the solutions that the OP marked as answers 7 years ago it would appear that they wanted this either for a WinForms solution or in the code behind because they wanted to do something IF the textbox was blank - all this does is say that the field is required, so may not actually cover the requirements. I've never seen the required attribute used in that way - are you sure that is correct?
Prateek Ghosh 3-May-18 8:23am    
question was how to know text box is empty so i didn't know weather it is for web or for windows it works fine for web app it is safe and easy and doesn't need to check on server weather it is empty.

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