You'll need to create a form with a textbox as you'll need to handle the keypress event of the textbox and thats something you can't do with an input box.
You can't apply any validation with the input box until after you have the whole string input. Checking that a whole string only contains alphabetic letters is where you'd use regular expression.
The validation technique with the keypress validates the key as soon as it's pressed. Your only allowing the string to be made up of alphabetic letters.
The input box isn't very good. Your better creating your own dialog and having more control over what it looks like and what it does.
Is there code to test for just plain alphabets though i.e. ADF, etc....
I want to test to see if they've entered alphabets. The length of the input does not really matter. I'm trying to prevent the user from entering numbers, symbols.
Just thought I'd share that I ended up using OR instead of AND.
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii < 65 OR KeyAscii > 90 Then 'invalid input, set to zero (ignore) KeyAscii = 0 End If End Sub
You can use Unicode Character Classes with regular expressions to check for alphabetic characters. The characters represented in Unicode are classed into different categories like, letters, numbers, currency symbols, mathmatical symbols etc. You can check whether a character belongs to certain category, so for example you want to check that the character is in the letter category.
Which you can do with this regular expression. \p{L}
Actually... you don't need to use regular expressions at all... there's a far easier way to do this.
In the key press event you want to check the ASCII code of the key being pressed and make sure it falls within the ASCII code of your valid characters.
Look for the ASCII range you need the character to match.. for A to Z the ascii code should be between 65 to 90.
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii < 65 And KeyAscii > 90 Then 'invalid input, set to zero (ignore) KeyAscii = 0 End If End Sub
Lower case and upper case characters are seperate.
The recommendation to use regular expression would be if you needed more powerful validation, like all currency symbols (dollars, pound, yen, euro, dong, etc) for example, or email addresses. The pattern I posted before would be if you wanted any valid letter in any language (chinese characters or english characters)
How do you test a user's input for alphabets?
Ghalib
Hi again,
You'll need to create a form with a textbox as you'll need to handle the keypress event of the textbox and thats something you can't do with an input box.
You can't apply any validation with the input box until after you have the whole string input. Checking that a whole string only contains alphabetic letters is where you'd use regular expression.
The validation technique with the keypress validates the key as soon as it's pressed. Your only allowing the string to be made up of alphabetic letters.
The input box isn't very good. Your better creating your own dialog and having more control over what it looks like and what it does.
vinay dubey
Thanks Chris.
Is there code to test for just plain alphabets though i.e. ADF, etc....
I want to test to see if they've entered alphabets. The length of the input does not really matter. I'm trying to prevent the user from entering numbers, symbols.
David NL
Wonderful!
Thank you Derek and Chris!
Andrey Egorov
Just thought I'd share that I ended up using OR instead of AND.
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 65 OR KeyAscii > 90 Then
'invalid input, set to zero (ignore)
KeyAscii = 0
End If
End Sub
DocJames
Hello.
You can accomplish this by utilizing regular expressions. Here is a brief article one how this is accomplished in VBA.
http://www.mvps.org/access/modules/mdl0063.htm
Here is a site to test your regular expression (has some nice tutorials too)
http://regexlib.com/RETester.aspx
Andrew Walaszek
Hi,
You can use Unicode Character Classes with regular expressions to check for alphabetic characters. The characters represented in Unicode are classed into different categories like, letters, numbers, currency symbols, mathmatical symbols etc. You can check whether a character belongs to certain category, so for example you want to check that the character is in the letter category.
Which you can do with this regular expression. \p{L}
Greg Leepart
Hmmm. Okay. I'm not really code savvy but can I simply use it like this
Do while variable <> "\p{L}"
STUFF
Loop
Thank you.
T. J. Bussler
Actually... you don't need to use regular expressions at all... there's a far easier way to do this.
In the key press event you want to check the ASCII code of the key being pressed and make sure it falls within the ASCII code of your valid characters.
http://www.lookuptables.com/
Look for the ASCII range you need the character to match.. for A to Z the ascii code should be between 65 to 90.
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 65 And KeyAscii > 90 Then
'invalid input, set to zero (ignore)
KeyAscii = 0
End If
End Sub
Lower case and upper case characters are seperate.
The recommendation to use regular expression would be if you needed more powerful validation, like all currency symbols (dollars, pound, yen, euro, dong, etc) for example, or email addresses. The pattern I posted before would be if you wanted any valid letter in any language (chinese characters or english characters)
Javier Carvajal
I see! This looks like it might work.
Would I be able to apply this code to an Inputbox or would I need to create a form with a textbox
Thank you!