Click here to Skip to main content
15,868,141 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");
                
            }
Posted
Updated 3-Apr-20 12:59pm
v2

First off, names can contain anything: "John Henderson-Allan" is a valid name. So are "StopFortnumAndMasonFoieGras Cruelty.com" 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:
C#
Regex regex = new Regex("^[a-zA-Z]+$");
bool hasOnlyAlpha = regex.IsMatch(myTextBox.Text);
 
Share this answer
 
Comments
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]")
 
Share this answer
 
Comments
Suren97 4-Mar-18 6:26am    
Thanks :)
Dim testVar As Object = Tele.Text
Dim numericCheck As Boolean
numericCheck = IsNumeric(testVar)
If numericCheck = True Then
Else
Tele.Text = ""
End If
 
Share this answer
 

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