限制文本框使用特殊字符和数字 [英] Restrict textbox from Special Characters and Numbers

查看:84
本文介绍了限制文本框使用特殊字符和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我有两个用于输入用户名和密码的文本框,我希望用户限制特殊字符和数字...

我已经编写了以下代码,但它不接受退格键和空格键.
如果用户要编辑用户名和密码,则退格按钮不起作用.
我在KeyPress事件中编写了这段代码.


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.


if (!char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }

推荐答案

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;
}



希望对您有所帮助!

问候,

曼弗雷德(Manfred)



Hope that helps!

Regards,

Manfred


尝试一下:-
Try this :-
if(e.KeyChar != 8 && e.KeyChar != 32)
      {
        if (!char.IsLetter(e.KeyChar))
            {
                e.Handled = true;
            }
       }



希望对您有帮助:)



Hope this will help you :)


这篇关于限制文本框使用特殊字符和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆