Answered by:
how can i check not allow user enter symbol and number in textbox?

Question
-
how can i check not allow user enter space, symbol and number in textbox?
- Edited by colton_chan Wednesday, March 13, 2013 7:45 PM
Wednesday, March 13, 2013 7:44 PM
Answers
-
Hello colton_chan,
how can i check not allow user enter space, symbol and number in textbox?
must be managed keypress and keydown events inside them then you can recover by clause, and this parameter via the KeyData property and keyvalue will return the key that the user pressed and the comparisons with the enumeration keys, see the documentation on msdn
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
- Carmelo La Monica
- Visual Basic Tips e Tricks Blog
- WordPress.com Blog
- Blogger
- CrystalwebDotNetGroup
- Proposed as answer by Frank L. Smith Wednesday, March 13, 2013 8:42 PM
- Marked as answer by Youen Zen Tuesday, March 26, 2013 8:05 AM
Wednesday, March 13, 2013 8:18 PM -
Additionally, you can also use a MaskedTextBox and then set the Mask property.
Please call me Frank :)
- Proposed as answer by Carmelo La Monica Wednesday, March 13, 2013 9:18 PM
- Marked as answer by Youen Zen Tuesday, March 26, 2013 8:05 AM
Wednesday, March 13, 2013 8:42 PM -
Put this in the textbox's KeyPress event:
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 'If Not Char.IsLetter(e.KeyChar) Then e.Handled = True 'will work with any upper or lowercase letter If Not (e.KeyChar >= Chr(65) And e.KeyChar <= Chr(90)) And TextBox1.SelectionStart = 0 Then e.Handled = True 'first letter must be uppercase If Not (e.KeyChar >= Chr(97) And e.KeyChar <= Chr(122)) And TextBox1.SelectionStart > 0 Then e.Handled = True 'next letters must be lowercase If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace End Sub
I commented out the first statement since it will accept any upper or lowercase letter.
The second statement will only allow an uppercase letter for the first character.
The third will allow only lowercase letters for the rest of the characters.
The last statement will allow a backspace.
Alternatively, if you are looking for the proper case of any name, use only the first and last statements in the textbox, and add the following to a button click event to correct the input:
firstname = StrConv(TextBox1.Text), VbStrConv.ProperCase)
You could even do this, which will change the text to ProperCase when the Enter key is pressed:
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Not Char.IsLetter(e.KeyChar) Then e.Handled = True 'will work with any upper or lowercase letter If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace If e.KeyChar = Chr(13) Then TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase) 'when Enter key is pressed End Sub
However, you could put the last statement inside the Leave event of the textbox, in case the user doesn't press the enter key but uses the mouse to move to another control:
Private Sub TextBox1_Leave(sender As Object, e As System.EventArgs) Handles TextBox1.Leave TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase) End Sub
Solitaire
Thursday, March 14, 2013 4:20 AM
All replies
-
Hello colton_chan,
how can i check not allow user enter space, symbol and number in textbox?
must be managed keypress and keydown events inside them then you can recover by clause, and this parameter via the KeyData property and keyvalue will return the key that the user pressed and the comparisons with the enumeration keys, see the documentation on msdn
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
- Carmelo La Monica
- Visual Basic Tips e Tricks Blog
- WordPress.com Blog
- Blogger
- CrystalwebDotNetGroup
- Proposed as answer by Frank L. Smith Wednesday, March 13, 2013 8:42 PM
- Marked as answer by Youen Zen Tuesday, March 26, 2013 8:05 AM
Wednesday, March 13, 2013 8:18 PM -
Additionally, you can also use a MaskedTextBox and then set the Mask property.
Please call me Frank :)
- Proposed as answer by Carmelo La Monica Wednesday, March 13, 2013 9:18 PM
- Marked as answer by Youen Zen Tuesday, March 26, 2013 8:05 AM
Wednesday, March 13, 2013 8:42 PM -
if I want to check first name is valid or not...
I use
NameCheck =
"^[A-Za-z]?"
Regex.IsMatch(txtFirstName.Text, NameCheck)
but it doesn't work
- Edited by colton_chan Thursday, March 14, 2013 3:45 AM
Thursday, March 14, 2013 3:45 AM -
Put this in the textbox's KeyPress event:
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 'If Not Char.IsLetter(e.KeyChar) Then e.Handled = True 'will work with any upper or lowercase letter If Not (e.KeyChar >= Chr(65) And e.KeyChar <= Chr(90)) And TextBox1.SelectionStart = 0 Then e.Handled = True 'first letter must be uppercase If Not (e.KeyChar >= Chr(97) And e.KeyChar <= Chr(122)) And TextBox1.SelectionStart > 0 Then e.Handled = True 'next letters must be lowercase If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace End Sub
I commented out the first statement since it will accept any upper or lowercase letter.
The second statement will only allow an uppercase letter for the first character.
The third will allow only lowercase letters for the rest of the characters.
The last statement will allow a backspace.
Alternatively, if you are looking for the proper case of any name, use only the first and last statements in the textbox, and add the following to a button click event to correct the input:
firstname = StrConv(TextBox1.Text), VbStrConv.ProperCase)
You could even do this, which will change the text to ProperCase when the Enter key is pressed:
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Not Char.IsLetter(e.KeyChar) Then e.Handled = True 'will work with any upper or lowercase letter If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace If e.KeyChar = Chr(13) Then TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase) 'when Enter key is pressed End Sub
However, you could put the last statement inside the Leave event of the textbox, in case the user doesn't press the enter key but uses the mouse to move to another control:
Private Sub TextBox1_Leave(sender As Object, e As System.EventArgs) Handles TextBox1.Leave TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase) End Sub
Solitaire
Thursday, March 14, 2013 4:20 AM