如题。
难道要每一个TextBox都验证过滤一遍吗?
那不是很麻烦?
有没有简单点的,统一禁止输入?

解决方案 »

  1.   

    在HttpModule事件中处理
    接收Request.form或request.querystring[]然后过滤
    这样每个页面如果有Requst.form或querystring都会检测,属于遍地撒网
    我只想到这个方法如果要禁止输入的话,只想到onblur
      

  2.   

    其实你可以写个通用js
    直接在js中判断
    把每个页面要用到过滤非法字符串的textbox命名统一
    这样只要直接引用js就可以!
      

  3.   


    也可以只写一个通用方法,传参数确定要验证的Text
      

  4.   

    为何不尝试一下jQuery的验证插件
      

  5.   

    如果是不能数据html标签 可以用正则表达式 来验证也可以用Server.htmlEncode()将html 编码。
    这样的内容输出到页面就不是标签,而成为文本了。
      

  6.   

    建议用正则表达式来控制textBox的值
      

  7.   

    综合一下大家的意见,
    我采用了三种方法分别处理:
    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);
        }