Click here to Skip to main content
14,665,095 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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:
Regex regex = new Regex("^[a-zA-Z]+$");
bool hasOnlyAlpha = regex.IsMatch(myTextBox.Text);
   
Comments
Suren97 4-Mar-18 6:26am
   
Thanks :)
OriginalGriff 4-Mar-18 6:27am
   
You're welcome!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Dont allow user to enter number in text box using javascript or using Regular expression
System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[ ^ 0-9]")
   
Comments
Suren97 4-Mar-18 6:26am
   
Thanks :)
Rate this:
Please Sign up or sign in to vote.

Solution 3

Dim testVar As Object = Tele.Text
Dim numericCheck As Boolean
numericCheck = IsNumeric(testVar)
If numericCheck = True Then
Else
Tele.Text = ""
End If
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100