如何阻止或限制文本框中的特殊字符 [英] How to block or restrict special characters from textbox

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

问题描述

我需要从文本框中排除特殊字符(%,&,/,,'等)

I need exclude special characters (%,&,/,",' etc ) from textbox

有可能吗?我应该使用key_press事件吗?

Is it possible? Should I use key_press event?

string one = radTextBoxControl1.Text.Replace("/", "");
                string two = one.Replace("%", "");
                //more string
                radTextBoxControl1.Text = two;

在这种模式下非常长=(

in this mode is very very long =(

推荐答案

我假设您尝试仅保留字母数字和空格字符.添加这样的按键事件

I am assuming you are trying to keep only alpha-numeric and space characters. Add a keypress event like this

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    var regex = new Regex(@"[^a-zA-Z0-9\s]");
    if (regex.IsMatch(e.KeyChar.ToString()))
    {
        e.Handled = true;
    }
}

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

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