Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Send sample coding for checking the TextBox input value as A-z or a-z
Posted
Updated 24-Nov-10 1:41am
v3
Comments
Toli Cuturicu 24-Nov-10 11:12am    
Do you think that we are your employees...? Because you command us what to do...

Hi,

maybe you could use regular expressions:

private void textBox1_TextChanged(object sender, EventArgs e)
{
  Regex re = new Regex("^[-'a-zA-Z]*$");
  if (re.IsMatch(textBox1.Text))
    MessageBox.Show(Test success - only valid characters");
}
 
Share this answer
 
Comments
Dalek Dave 24-Nov-10 7:49am    
Good Answer.
shaikh-adil 19-Nov-12 1:01am    
+5
Or do it more verbosely: :-D

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    Char c = e.KeyChar;
    int i = (int) c;
    int sa = (int) 'a';
    int sz = (int) 'z';
    int ba = (int) 'A';
    int bz = (int) 'Z';
    if (! ((i >= sa && i <= sz) || (i >= ba && i <= bz)))
    {
        e.Handled = true;
    }
}


Setting Handled property on KeyPressEventArg to true, keeps the event vom propagating thus swallowing the input.

Cheers

Manfred
 
Share this answer
 
v4
Simply use regex, like this:
string s = "this is a test";

Regex regex=new Regex("[^a-zA-Z]");
if(regex.IsMatch(s)) { ... is alpha ...}
}


good luck!
 
Share this answer
 
Here's some LINQ:

C#
string myString = "MyStringText";
string myString2 = "My String Text";
int A = Convert.ToInt('A');
int Z = Convert.ToInt('Z');
bool isAtoZ = false;

var count = (from ch in myString.ToUpper()
             where (int)ch >= A && (int)ch <= Z
             select ch).Count();

isAtoZ = (count == myString.Length);

var count2 = (from ch in myString2.ToUpper()
              where (int)ch >= A && (int)ch <= Z
              select ch).Count();

isAtoZ = (count2 == myString.Length);


Using Regex (sample already provided)

Using just the string:

string myString = "MyStringText";
bool isAtoZ = true;
foreach(char ch in myString.ToUpper())
{
    if ((int)ch < Convert.ToInt('A') || (int)ch > Convert.ToInt('Z'))
    {
        isAtoZ = false;
        break;
    }
}


Using helper objects with LINQ:

C#
string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string myString = "MYStringText";
bool isAtoZ = true;
var count = (from ch in myString.ToUpper()
             where validChars.Contains(ch)
             select ch).Count();
isAtoZ = (count == myString.Length);


As you can see, there are a million ways to do the same thing, and if you had been paying attention in class, you would realize that your instructor gave you all the knowlege and reference material to accomplish your homework assignment. Even if you claim it's NOT a homework assignment, considering you had to ask for help with such a simple task should be an indication that maybe computer programming ain't exactly your calling in life, and maybe society would be better off if you were to become a wet paint monitor.
 
Share this answer
 
v4
Comments
Manfred Rudolf Bihy 24-Nov-10 8:05am    
Cat got your code? (May also have been the dog) :-D
JF2015 24-Nov-10 8:30am    
Very good answer - have my 5!
Manfred Rudolf Bihy 24-Nov-10 18:13pm    
This code looks fundamentally different from what I commented on first. Hmmm, me thinks I have to annnotate my comments with the answers revision code. The version I commented on originally was somehow mangled.

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