Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.22/5 (4 votes)
See more:
hii
I dont want to allow all space in textbox in c# windows applications means if i give all spaces in textbox the form should not submit

thank you
Regards
Mahesh
Posted
Comments
[no name] 13-Oct-12 10:58am    
Okay so don't allow all spaces. What is the problem?
bbirajdar 13-Oct-12 14:27pm    
Yes.. I too don't see any problem.. Where's the question?

Handle the TextBox.TextChanged Event.
In it:
C#
private void myTextBox_TextChanged(object sender, EventArgs e)
    {
    TextBox tb = sender as TextBox;
    if (tb != null)
        {
        myOKButton.Enabled = !string.IsNullOrWhiteSpace(tb.Text);
        }
    }
 
Share this answer
 
Comments
bbirajdar 13-Oct-12 14:28pm    
@OG .. I think the tb.Text needs a .Trim()..
OriginalGriff 13-Oct-12 15:33pm    
Nope - that's the difference between IsNullorEmpty and IsNullOrWhietspace (introduced at .NET 4.0) :D
bbirajdar 13-Oct-12 15:41pm    
Then it deserves a 5 :)
bbirajdar 13-Oct-12 15:42pm    
Somebody voted a nasty 1.. Sick...!!!
hi,
here is the small code for you, write the following code under validating event procedure of the textBox.
C#
private void mytextBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (string.IsNullOrWhiteSpace(tb.Text) == true)
            {
                MessageBox.Show("Your error message for users");
                e.Cancel = true;
                return;
            }
        }
 
Share this answer
 
For .Net Framework 3.0 and lower, you can use String.IsNullOrEmpty(string) function which returns true if the string argument is Null or is empty. But this function does not check if the string parsed contains only spaces(WhiteSpace), so you you also have to call the Trim() function of string.You can create a validate function to check your controls before remitting it...

C#
public bool ValidateControls()
        {
            if (String.IsNullOrEmpty(myTextBox.Text))
            {
                //Tell the user that the text provided is unacceptable.
                MessageBox.Show("The content of the Textbox is not valid.");
                //Validation was unsuccessful.
                return false;
            }
            //Validation was successful.
            return true;
        }


However, if you're using .Net Framework 3.5 and higher, you should use String.IsNullOrWhiteSpace(string) function which returns true if the string argument is empty, Null or contains only spaces. So, you would not have to Trim the string again, and the above code could be rewritten as

C#
public bool ValidateControls()
        {
            if (String.IsNullOrWhiteSpace(myTextBox.Text))
            {
                //Tell the user that the text provided is unacceptable.
                MessageBox.Show("The content of the Textbox is not valid.");
                //Validation was unsuccessful.
                return false;
            }
            //Validation was successful.
            return true;
        }
 
Share this answer
 
v4
Hi,
There are two excellent methods included in .NET framework:

1. String.IsNullOrWhiteSpace() Method[^] available starting from .NET 4.0
2. String.IsNullOrEmpty Method[^] available starting from .NET 2.0

The use of (1) is recommended, like: String.IsNullOrWhiteSpace(TextBox1.Text), though in legacy applications (prior to .NET 4.0) the other option, as specified at Microsoft MSDN (2), is to use the expression: String.IsNullOrEmpty(value) || value.Trim().Length == 0;. The latter is essentially equivalent to (1), but (1) has better performance.

I hope this will help.
Kind regards,
AB
 
Share this answer
 
Though you don't provide enough information about your form.So i assume you have a form of different textboxes and also may contain a Save button to save all those info.
So you may add this to your button event..
C#
private void button1_Click(object sender, EventArgs e)
{
    if (textBox2.Text != "")
    {
        //do your saving information code here
    }
    else
    {
        //you can provide notification for user here
        MessageBox.Show("Please fill up all textbox information");
    }
}
 
Share this answer
 
I believe, you should put a custom validator with regex as to now allow all space i.e. atleast one character or numeric as per your requirement. For example, the below one is asking for atleast one character and one number.

^.*(?=.{4,10})(?=.*\d)(?=.*[a-zA-Z]).*$


Or the below regex will check for any space
^[\S]*$


Now, unless the user enters at-least one non-space character, the form will not submit. This will also avoid any server trip which will be the case if you use button click, keypress or text change event.

Hope that helps
Milind
If the answer helps, mark it as solution/upvote.
 
Share this answer
 
C#
if(textbox1.Text.Trim() == "") 
{
    MessageBox.Show("Error", "Error");
}
 
Share this answer
 
v2

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