Share via


textBox with no spaces allowed

Question

Tuesday, November 17, 2009 9:18 AM

How to define for the textBox, that there are no spaces allowed (only letters, numbers, or signs).  

I mean the user has to enter his name into textBox, but he only has to enter his name (for example). Just one word together. No spaces at all.

If user enters for example : "John", thats ok, if user enters: "John A" it has to warn him of incorrect input.

How to define that?

All replies (9)

Tuesday, November 17, 2009 9:41 AM ✅Answered | 1 vote

Should create a custom control which derived from TextBox and you need to catch events: KeyDown and Leave.
Using KeyDown event to remove Space char:

protected override void OnKeyDown(KeyEventArgs e)
        {
            Keys key = e.KeyCode;
            if (key == Keys.Space)
            {
                e.Handled = true;
            }

            base.OnKeyDown(e);
        }

Using Leave event to warn the user:

protected override void OnLeave(EventArgs e)
        {
            if (this.Text.Contains(" "))
            {
                MessageBox.Show("Don't accept Space char in your name");
                this.Focus();
            }
        }

Tuesday, November 17, 2009 10:14 AM ✅Answered

What about this:

Regex Check1 = new Regex(@"\s");
string NameChecking = textBoxName.Text;
if (Check1.IsMatch(NameChecking))
{
    MessageBox.Show("You have entered two letters into text box");
}

it works :)


Tuesday, November 17, 2009 12:23 PM ✅Answered | 1 vote

Hi.
Here is one more (I think it is the most simple way...):

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Contains(" "))
                MessageBox.Show("No Spaces!");
        }

Noam B
______________________________________________________________________

Do not Forget to Vote as Helpful, Please. It encourages us to help you...


Tuesday, November 17, 2009 9:25 AM

foreach (char c in textBox1.Text.ToCharArray())
            {
                if (char.IsWhiteSpace(c) == true)
                {
                    // Warn User
                }
            }

Tuesday, November 17, 2009 9:46 AM

I would prefer the KeyPress event of the Textbox.

Here you can check the pressed key and if it is a whiteSpace you can show the user imediately your warning.

And if the user wants to enter the whitespace, you can set the handled property to true, so that the whitespace
will not appeat in the textbox.

This solution is in my eyes better than checking the string after entering it.

 

 

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
  if (e.KeyChar == ' ')
    {
      MessageBox.Show("Error no whitespaces");
      e.Handled = true;
    }
}

Florian Kowalsky


Tuesday, November 17, 2009 10:22 AM

Ok i think there will be a lot of solutions, and you have to decide which one you prefer.

If it works it ok, but you have to know what you want.

Has the user to correct his entry or should your code not allow to enter a whitespace?
At first you have to decide this and then take the right solution.

 


Florian Kowalsky


Tuesday, November 17, 2009 10:46 AM | 1 vote

private

void txtSVat_KeyPress(object sender, KeyPressEventArgs e)

{
if (e.KeyChar == ' ')
{
//Warn the user.
e.KeyChar = '\0';
}
}


Saturday, April 22, 2017 6:46 AM

This disables typing whitespaces:

            textBox1.Text = textBox1.Text.Trim();
            textBox1.SelectionStart = textBox1.Text.Length; 
            textBox1.SelectionLength = 0;

Saturday, December 23, 2017 7:22 AM

  All Form Apply No space Allowed

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Space)
            {

                return true;
            }
           return base.ProcessCmdKey(ref msg, keyData);
        }

Only Copy And Paste This Function....and Run