如何防止用户使用textchanged事件C#在文本框中放置任何特殊字符? [英] How to prevent user to put any special character in the textbox using textchanged event C#?
本文介绍了如何防止用户使用textchanged事件C#在文本框中放置任何特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这适用于网页应用程序我不想使用javascript
我尝试过:
protected void TextBox1_TextChanged(object sender,EventArgs e)
{
string text =( (TextBox)sender).Text;
var regex = new Regex(@[^ a-zA-Z0-9 \ s]);
if( !regex.IsMatch(text.ToString()))
{
返回false;
}
}
this is apply on web application i don't want to use javascript
What I have tried:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string text = ((TextBox)sender).Text;
var regex = new Regex(@"[^a-zA-Z0-9\s]");
if (!regex.IsMatch(text.ToString()))
{
return false;
}
}
推荐答案
甚至不尝试做这个服务器端 - 这不仅意味着每次用户键入一个密钥时往返服务器,这实际上是真的打乱打字速度,这是一种非常糟糕的思考方式,即使你能做到这一点,你也做不到。
你需要在Javascript中这样做: 如何通过javascript限制文本框中的特殊字符和空格? [ ^ ]
Don't even try to do this server side - not only would it mean a round trip to the server every time the user types a key, which would really mess up typing speeds, it's a spectacularly bad way to think, even if you could do it, which you can't.
You need to do this in Javascript: How to restrict special characters and spaces in textbox via javascript?[^]
这篇关于如何防止用户使用textchanged事件C#在文本框中放置任何特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文