防止恶意字符输入

来源:互联网 发布:怎样装修淘宝店铺 编辑:程序博客网 时间:2024/04/30 10:03
  public static string InputText(string text, int maxLength)
        
{
            
if (string.IsNullOrEmpty(text))
                
return string.Empty;
            text 
= text.Trim();
            
if (string.IsNullOrEmpty(text))
                
return string.Empty;
            
if (text.Length > maxLength)
                text 
= text.Substring(0, maxLength);
            text 
= Regex.Replace(text, "[/s]{2,}"" ");    //two or more spaces
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|/n)*?>)"" ");    //<br>
            text = Regex.Replace(text, "(/s*&[n|N][b|B][s|S][p|P];/s*)+"" ");    //&nbsp;
            text = Regex.Replace(text, "<(.|/n)*?>"string.Empty);    //any other tags
            text = text.Replace("'""''");
            
return text;
        }
 
原创粉丝点击