如题,下面是 代码using System;
using System.Text;namespace HuaXiaTG.WebUI
{
/// <summary>
/// A sample class to clean the input into web pages 
/// </summary>
public sealed class CleanString { public static string InputText(string inputString, int maxLength) {
StringBuilder retVal = new StringBuilder(); // check incoming parameters for null or blank string
if ((inputString != null) && (inputString != String.Empty)) {
inputString = inputString.Trim(); //chop the string incase the client-side max length
//fields are bypassed to prevent buffer over-runs
if (inputString.Length > maxLength)
inputString = inputString.Substring(0, maxLength); //convert some harmful symbols incase the regular
//expression validators are changed
for (int i = 0; i < inputString.Length; i++) {
switch (inputString[i]) {
case '"':
retVal.Append("&quot;");
break;
case '<':
retVal.Append("&lt;");
break;
case '>':
retVal.Append("&gt;");
break;
default:
retVal.Append(inputString[i]);
break;
}
} // Replace single quotes with white space
retVal.Replace("'", "");
} return retVal.ToString();

}
}
}过滤了这些字符,就万事OK了吗??对数字型的怎么办?

解决方案 »

  1.   

    这好像是自己做的HtmlEncode最后的// Replace single quotes with white space
    retVal.Replace("'", "");
    }
    可能有点用
      

  2.   

    不是我做的,,,从potshop拷过来的
      

  3.   

    对,防注入,防用户输入Html字符,控制用户输入的长度
      

  4.   

    恩,不是potshop吧,害我第一眼看起来是photoshop呢,还以为是编辑软件,是
    petshop吧,很多教科书也都是引用改方法的
      

  5.   

    值得学习,不过我学的是txtname.Text.Replace("'","").Replace("<","").Replace(">","").Replace("AND","").Repace("Or","");不知道这样有什么缺点?
      

  6.   

    不要相信传来的任何参数.过滤字符串是一法.数值的也不能相信,一律按实际需要的类型转换
    int.Parse() double.Parse()等等.
      

  7.   

    /// <summary>
    /// 去除非法字串
    /// </summary>
    /// <param name="strChar">原字串</param>
    /// <returns>过滤过的字串</returns>
    public static string ReplaceBadChar(string strChar) 

    if (strChar.Trim() == "") 

    return "";

    else 

    strChar=strChar.Replace("'","");
    strChar=strChar.Replace("*","");
    strChar=strChar.Replace("?","");
    strChar=strChar.Replace("(","");
    strChar=strChar.Replace(")","");
    strChar=strChar.Replace("<","");
    strChar=strChar.Replace("=","");
    return strChar.Trim();

    }