过滤非法字符~ 最近发现用户在从网上复制数据然后粘贴入OA,添加数据时报错~比如复制几个字“为革命,保护视力”,粘贴后变成“为革命,保护视力 ”,本以为是多了个空格,但跟踪查看后发现是“燩”,十分费解,,,,,不知是何原因???? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 单步调试看看数据过滤在后台设置过滤字符void Application_BeginRequest(object sender, EventArgs e){ for (int i=0; i < Request.Form.Count;i++) { if (Request.Form[i].ToString() == "__VIEWSTATE") continue; if (IsM(Request.Form[i].ToString())) { Response.Write("您提交的内容中含有非法字符."); Response.End(); } } }protected bool IsM(string InText){ string word = @""; if (InText == null) return false; if (Regex.IsMatch(InText,word)) return true; return false;} 木有看明白:protected bool IsM(string InText){ string word = @""; if (InText == null) return false; if (Regex.IsMatch(InText,word)) return true; return false;} 难道"燩"就是个空格??? /// <summary> /// 过滤输入信息 /// </summary> /// <param name="text">内容</param> /// <param name="maxLength">最大长度</param> /// <returns></returns> /// 替换不正常字符 /// 记得引用 using System.Text.RegularExpressions; public static string InputText(string text, int maxLength) { #region 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)*?>)", "\n"); //<br> text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags text = text.Replace("'", "''"); return text; #endregion } 说实话,我也替换过了,数据存储类型为ntext,可就是替不了“燩”, 它在输入框中看不到,是以空格的形式存在,只有在断点跟入时才看到它,但也就报错了~ 使用一个文本编辑器吧 不知道你使用的是不是 这个通常不会出错的 哪怕有特殊字符 要是不行的话 你试试 用trim() 应该可以的 是否编码的问题呢?SQL Server 的处理可以在字符串前加N 怎么直接把PetShop的代码贴过来了… 如果是登陆或者注册的话可以看看web.config文件中Membership配置节中的内容。 就算是一个textbox中用户输入了乱码,也不至于运行出错吧?难道我真的out了 Jquery 弹出层如何获取父窗口 asp.net+mysql存储分页问题 请教各位高手关于投票的功能实现 OCX控件问题,页面根本不显示,也不提示错误 .net也能挣 4K吗????????? 关于报表自动生成时控件位置控制的问题 事务处理中的问题 ASP.NET不能建立WEB应用程序是怎么回事呀 完了,完了,现在做系统没感觉了. 奇怪!VS.NET编写ASP.NET程序竟然没有代码即时提示功能! 视频怎么上传和加密 哪位高手可以帮我注释下DBHelper
过滤
在后台设置过滤字符
void Application_BeginRequest(object sender, EventArgs e)
{
for (int i=0; i < Request.Form.Count;i++)
{
if (Request.Form[i].ToString() == "__VIEWSTATE") continue;
if (IsM(Request.Form[i].ToString()))
{
Response.Write("您提交的内容中含有非法字符.");
Response.End();
} }
}
protected bool IsM(string InText)
{
string word = @"";
if (InText == null)
return false;
if (Regex.IsMatch(InText,word))
return true;
return false;
}
木有看明白:
protected bool IsM(string InText)
{
string word = @"";
if (InText == null)
return false;
if (Regex.IsMatch(InText,word))
return true;
return false;
}
难道"燩"就是个空格???
/// 过滤输入信息
/// </summary>
/// <param name="text">内容</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
/// 替换不正常字符
/// 记得引用 using System.Text.RegularExpressions;
public static string InputText(string text, int maxLength)
{
#region
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)*?>)", "\n"); //<br>
text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //
text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
text = text.Replace("'", "''");
return text;
#endregion
}
SQL Server 的处理可以在字符串前加N