Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Friends,

I have two textbox for username and password ,i want user to restrict special characters and numbers...

i have written the following code but it is not accepting backspace and space buttons.
if the user wants to edit the username and password, backspace button is not working.
This code i have written in KeyPress Event.


C#
if (!char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
Posted
Updated 18-Sep-20 1:18am

C#
if (char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space)
{
    // These characters may pass
    e.Handled = false;
}
else
{
    // Everything that is not a letter, nor a backspace nor a space will be blocked
    e.Handled = true;
}


Hope that helps!

Regards,

Manfred
 
Share this answer
 
Comments
Rockstar_ 11-May-12 8:03am    
Thank You ur code solved my problem...
Manfred Rudolf Bihy 11-May-12 8:19am    
You're welcome!
If it solved your problem I'd appreciate a vote. :)
BillW33 11-May-12 9:23am    
You got a +5 from me. :)
Try this :-
if(e.KeyChar != 8 && e.KeyChar != 32)
      {
        if (!char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
       }


Hope this will help you :)
 
Share this answer
 
Comments
Rockstar_ 11-May-12 8:05am    
thanks...

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