//验证用户输入的字符串中时候只含有数字或字母,汉字 bool IsValidString(string strIn) { return Regex.IsMatch(strIn,@"^[A-Za-z0-9\u4e00-\u9fa5]+$"); } //验证电子邮件 bool IsValidEmail(string strIn) { return
Regex.IsMatch(strIn,@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } //验证用户输入的字符串中时候只含有数字或字母 bool IsValidString(string strIn) { return Regex.IsMatch(strIn,@"^[A-Za-z0-9]+$"); } ///<summary> /// 判断密码 ///</summary> ///<param name="PassWord"></param> ///<returns></returns> publicstaticbool IsValidPassWord(string PassWord) { return Regex.IsMatch(PassWord, @"^(\w){6,20}$"); }
///<summary> /// 判断电话/传真 ///</summary> ///<param name="Tel"></param> ///<returns></returns> publicstaticbool IsValidTel(string Tel) { return Regex.IsMatch(Tel, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$"); }
///<summary> /// 判断手机 ///</summary> ///<param name="Mobil"></param> ///<returns></returns> publicstaticbool IsValidMobil(string Mobil) { return Regex.IsMatch(Mobil, @"^(\d)+[-]?(\d){6,12}$"); }
///<summary> /// 邮政编码 ///</summary> ///<param name="Zip"></param> ///<returns></returns> publicstaticbool IsValidZip(string Zip) { return Regex.IsMatch(Zip, @"^[a-z0-9 ]{3,12}$"); }
///<summary> /// 判断日期 ///</summary> ///<param name="Date"></param> ///<returns></returns> publicstaticbool IsValidDate(string Date) { bool bValid=Regex.IsMatch(Date, @"^[12]{1}(\d){3}[-][01]?(\d){1}[-][0123]?(\d){1}$"); return (bValid && Date.CompareTo("1753-01-01")>=0);
}
//判断整型 /*public static bool IsValidInt(string Int) { return Regex.IsMatch(Int, @"^[1-9]{1}[0-9]{0,6}$"); }*/
///<summary> /// 判断只能输字母 ///</summary> ///<param name="EnName"></param> ///<returns></returns> publicstaticbool IsValidEnName(string EnName) { return Regex.IsMatch(EnName, @"[a-zA-Z]"); }
//去除输入的字符串中不合法的<>\"'%;()& privatestring SanitizeInput(string input) { Regex badCharReplace =new Regex(@"^([<>""'%;()&])$"); string goodChars = badCharReplace.Replace(input, ""); return goodChars; }
//用正则表达式过滤脚本publicstring wipeScript(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(@"
on[\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); html = regex1.Replace(html, ""); //过滤<script></script>标记html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件html = regex4.Replace(html, ""); //过滤iframehtml = regex5.Replace(html, ""); //过滤framesetreturn html; }
//对输入的字符串是否含有<script></script>判断publicbool IsValidScript(string html) { return Regex.IsMatch(html,@"<script[\s\S]+</script *>"); }
//对输入的字符串是否含有href=javascript: (<A>)字符判断publicbool IsValidJavascript(string html) { return Regex.IsMatch(html,@" href *= *[\s\S]*script *:"); }
//对输入的字符串是否含有on...事件判断publicbool IsValidOn(string html) { return Regex.IsMatch(html,@" on[\s\S]*="); }
//对输入的字符串是否含有iframe判断publicbool IsValidiframe(string html) { return Regex.IsMatch(html,@"<iframe[\s\S]+</iframe *>"); }
//对输入的字符是否含有frameset判断publicbool IsValidFrameset(string html) { return Regex.IsMatch(html,@"<frameset[\s\S]+</frameset *>"); } 但是好像.net对这些有自己的判断