how to validate the textbox without allowing spaces in windows form application using C#.net in my project .i can validate the text box ,without allowing spaces.... in this two things ............. 1.only spaces are not allowed 2.after entering one or two characters textbox accept spaces...........
-
Refer http://stackoverflow.com/questions/2703461/regular-expression-alphanumerics-with-space http://stackoverflow.com/questions/924287/regular-expression-for-alphnumeric-and-space . You have to modify the regex to suit your requirement. – Sandeep G B May 13 '11 at 04:49
-
Do you mean that the user cannot start with `space` but can latter on use ? – V4Vendetta May 13 '11 at 04:54
-
Hi, Sorry I did not able to see your question first and I think its not a good way to answer two times in stackoverflow. So I would like to suggest you to refer the below link where I answered few minutes before http://stackoverflow.com/questions/5987924/validating-textbox-in-windows-form-applications/5988074#5988074 – SharpUrBrain May 13 '11 at 06:53
3 Answers
You can restrict the user from entering space in the TextBox
by handling the KeyPress
event
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == (char)Keys.Space);
}
EDIT
In case space is allowed after entering a character or two , then you should be using
textbox1.Text.TrimStart()

- 37,194
- 9
- 78
- 82
-
I think spaces are not allowed only at the beginning of the text but can be entered later. – Sandeep G B May 13 '11 at 04:51
-
doing a TrimStart() would work if the issue was only that we weren't allowing spaces at the start. However, the OP said that they wanted to have one or two characters before allowing a space. If it is one character you are fine. However, if it is two, this method won't work. – IAmTimCorey May 13 '11 at 04:59
-
@BiggsTRC How will that make a difference, `TrimStart` removes the space/spaces from the start after that one or two chars won't be a problem. – V4Vendetta May 13 '11 at 05:03
-
The OP said that it might require two characters before a space was allowed. In this case, "B oy" would not be valid. `TrimStart` would not address this issue. – IAmTimCorey May 13 '11 at 05:04
-
@BiggsTRC well he said **one or two characters**. I think only the OP would be able to clarify – V4Vendetta May 13 '11 at 05:06
If you don't want to allow any other characters entry except for the alphanumeric characters in a TextBox, then you can do this on the KeyPress event of a TextBox.
In the KeyPress Event, you need to check whether the entered character is either a Letter or a Digit.
Char.IsLetterOrDigit(e.KeyChar)
If yes, then allow the Key pressing by setting
"e.Handled = false"
Otherwise, don't allow the Key pressing by setting "e.Handled = true"
private void txtCardID_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsLetterOrDigit(e.KeyChar) // Allowing only any letter OR Digit
|| e.KeyChar == '\b') // Allowing BackSpace character
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}

- 2,078
- 1
- 20
- 16
It depends on what you mean by "validate". Winforms has the Validating
and Validate
events that fire when you leave a control. You could tap into these and validate your textbox then. However, if you want to validate as you type, you want to use the Key_Press
event to check every time a key is pressed to be sure the information in the box is still valid.
Here is a SO article on validation:
The answers in there give some different ideas depending on what you want to do. Whatever you decide, be sure to make sure you check the field properly. For example, if you use Key_Press
, don't just count how many characters are in the field before allowing a space. If you did that, the user could have moved the cursor to the beginning and pressed space. That would mess up your system. Make sure to check the entire field when you validate, even if you are using the Key_Press
event. Use RegEx with as complex a pattern as you want to do this.

- 1
- 1

- 16,412
- 5
- 39
- 75