如题。
难道要每一个TextBox都验证过滤一遍吗?
那不是很麻烦?
有没有简单点的,统一禁止输入?
难道要每一个TextBox都验证过滤一遍吗?
那不是很麻烦?
有没有简单点的,统一禁止输入?
解决方案 »
- 帮忙解释一下这个正则表达式?
- 请问,在ASP。NET中<%......%>与 <script...></script>这两个标记有什么区别???
- 请问怎么做一个树形目录???(解决给分)
- 上传问题。
- 请教各位高手一个问题!
- 在DataGrid中,如何判断当前是在父表中还是子表中呢?
- ***********DBGrid问题*************
- 不用框架,如何使右侧列固定,左侧列自适应宽度?
- 各位大哥:在asp.net里,怎么将页面发送到服务器上面?
- Please Ask:思归等高手,关于SqlCommandBuilder.DeriveParameters方法的性能问题
- 求租GridView1 怎么样传值绑定!!!
- GridView模板列里面的html控件问题
接收Request.form或request.querystring[]然后过滤
这样每个页面如果有Requst.form或querystring都会检测,属于遍地撒网
我只想到这个方法如果要禁止输入的话,只想到onblur
直接在js中判断
把每个页面要用到过滤非法字符串的textbox命名统一
这样只要直接引用js就可以!
也可以只写一个通用方法,传参数确定要验证的Text
这样的内容输出到页面就不是标签,而成为文本了。
我采用了三种方法分别处理:
1.对于要求纯文字的栏目,直接删除所有HTML标签(以后改一下,只保留文字);
2.对于带格式的内容,删除脚本标签<script></script>
3.普通内容,用Server.HtmlEncode()编码就算了,显示时也不再解码。这些是参考别人的代码: /// <summary>
/// 删除文本里的html标签和多余空格
/// (用在主题或者少量的文字)
/// </summary>
/// <param name="html">包含HTML的文本</param>
/// <returns></returns>
public static string RemoveHtml(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //过滤<script></script>标记
html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
html = regex4.Replace(html, ""); //过滤iframe
html = regex5.Replace(html, ""); //过滤frameset
html = regex6.Replace(html, ""); //过滤frameset
html = regex7.Replace(html, ""); //过滤frameset
html = regex8.Replace(html, ""); //过滤frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
} /// <summary>
/// 删除文本里的脚本和框架
/// (保留非危险的html标签,用在文章内容等)
/// </summary>
/// <param name="html">包含HTML的文本</param>
/// <returns></returns>
public static string RemoveScript(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //过滤<script></script>标记
html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
html = regex4.Replace(html, ""); //过滤iframe
html = regex5.Replace(html, ""); //过滤frameset
html = regex6.Replace(html, ""); //过滤frameset
html = regex7.Replace(html, ""); //过滤frameset
html = regex8.Replace(html, ""); //过滤frameset
// html = regex9.Replace(html, ""); //
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}
//或者 /// <summary>
/// 删除Html标签
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string ReplaceHtml(string text)
{
return Regex.Replace(text, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
}