How to allow textbox to enter only alphabets
-
hello all How to allow textbox to enter only alphabets using RegularExpression (Regex) i have added the namespace using System.Text.RegularExpressions; Thanks
-
hello all How to allow textbox to enter only alphabets using RegularExpression (Regex) i have added the namespace using System.Text.RegularExpressions; Thanks
Try handling the
KeyDown
event for theTextBox
. If the character is one you don't want (a digit or punctuation for example), swallow it by settinge.Handled
totrue
. That should work.Luis Alonso Ramos Intelectix Chihuahua, Mexico My Blog!
-
regex that allow only alphabets is below ^[a-zA-Z\S]+$. You can validate the entered text in textbox by this.
Cheers!! Brij
-
hello all How to allow textbox to enter only alphabets using RegularExpression (Regex) i have added the namespace using System.Text.RegularExpressions; Thanks
There's a bit more to this than you may think as you'll need to allow navigation and edit key combinations in your text box so just dropping the characters you don't want isn't enough. Also, you'll need to handle pasting of information to make sure it's valid. I wrote an article[^] that does something similar - only numbers - but it can easily be adapted. It doesn't handle text that may be placed programatically, but a little checking in OnTextChanged would handle that.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)