Click here to Skip to main content
14,739,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example i have textbox Name, and of course i can't write in it a number, because the name can't be number,how can i check it?

What I have tried:

string alph = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
            if (!textBox1.Text.Contains(alph))
                MessageBox.Show("This field only for letters", "Name");
Updated 3-Apr-20 13:59pm

First off, names can contain anything: "John Henderson-Allan" is a valid name. So are "StopFortnumAndMasonFoieGras" and "Brfxxccxxmnpcccclllmmnprxvclmnckssqlbb11116" - so you shouldn't ban anything in particular from a name, any more than you should ban any of these "!#$%&'*+-/=?^_`{|}~" from an email address.

But, to restrict a textbox to upper and lower case letters only just use a Regex:
Regex regex = new Regex("^[a-zA-Z]+$");
bool hasOnlyAlpha = regex.IsMatch(myTextBox.Text);
Suren97 4-Mar-18 6:26am
Thanks :)
OriginalGriff 4-Mar-18 6:27am
You're welcome!
Dont allow user to enter number in text box using javascript or using Regular expression
System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[ ^ 0-9]")
Suren97 4-Mar-18 6:26am
Thanks :)
Dim testVar As Object = Tele.Text
Dim numericCheck As Boolean
numericCheck = IsNumeric(testVar)
If numericCheck = True Then
Tele.Text = ""
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900