Click here to Skip to main content
14,826,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im using masked text in my form..I want to allow only alphabets in the masked textbox..i validated the text box for only allowing alphabets as the normal text box but it is allowing numbers..pls give the solution for it...

You're obviously not validating it properly. I tend to make my own control, because the masked text box just tells you when you entered something wrong, from memory. In the key press or key down event check Char.IsControl and Char.IsAlpha and if they both fail, reject the keypress.
premkumar.r 1-Feb-12 1:43am
private void txtSector_KeyPress(object sender, KeyPressEventArgs e)

txtSector.Mask = "AAA/AAA/AAA/AAA/AAA";
if (Char.IsLetter(e.KeyChar))
e.KeyChar = char.ToUpperInvariant(e.KeyChar);
txtSector.Text = txtSector.Text;

public static void validateAlphabetsOnly(KeyPressEventArgs e)
int ascii = Convert.ToInt16(e.KeyChar);
if ((ascii >= 97 && ascii <= 122) || (ascii >= 65 && ascii <= 90) || (ascii == 8))
e.Handled = false;
e.Handled = true;

we have validated correctly sir using the above events..we have put the validation for not allowing special characters and numbers..It is not allowing special characters but it is allowing numbers..Pls give any solutions..
I agree with this approach. It's the best, but only for simple criteria (filtering out some characters is simple).
My 5.
Use the Validating event[^] or KeyDown event and then write a simple regular expression to accept only alphabets.

Check this answer[^] out.

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