using System; using System.Collections.Generic; using System.Web; using System.Text.RegularExpressions; using System.Web.Security; using System.Web.Caching; using System.Data; using System.Text; using System.Web.UI; /// /// 网站一般通用方法 /// public abstract class Common { private static HttpResponse Response = HttpContext.Current.Response; private static HttpRequest Request = HttpContext.Current.Request; private static Cache Cache = HttpContext.Current.Cache; //来访者基本情况提问类型数组 public static readonly string[] MemberQuestionType = { "单项选择", "多项选择", "单行输入", "多行输入", "下拉列表" }; public Common() { // //TODO: 在此处添加构造函数逻辑 // } public static void Alert(string msg) { HttpContext.Current.Response.Write(""); HttpContext.Current.Response.End(); } /// /// 是否为非负整数 /// public static bool IsInteger(string number) { if (string.IsNullOrEmpty(number)) { return false; } else { Regex r = new Regex(@"^\d+$"); return r.IsMatch(number); } } /// /// 是否为合法的18位身份证号码 /// public static bool IsIDCard(string CardId) { if (string.IsNullOrEmpty(CardId)) { return false; } else if (CardId.Length < 18) { return false; } else { long n = 0; //数字验证 if (long.TryParse(CardId.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(CardId.Replace('x', '0').Replace('X', '0'), out n) == false) { return false; } //省份验证 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if (address.IndexOf(CardId.Remove(2)) == -1) { return false; } //生日验证 string Mybirth = CardId.Substring(6, 8).Insert(6, "-").Insert(4, "-"); DateTime Mytime = new DateTime(); if (DateTime.TryParse(Mybirth, out Mytime) == false) { return false; } //校验码验证 string[] MyVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(','); string[] wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(','); char[] ai = CardId.Remove(17).ToCharArray(); int sum = 0; for (int i = 0; i < 17; i++) { sum += int.Parse(wi[i]) * int.Parse(ai[i].ToString()); } int y = -1; Math.DivRem(sum, 11, out y); if (MyVarifyCode[y] != CardId.Substring(17, 1).ToLower()) { return false; } return true; } } /// /// 判断是否为合法的手机号码 /// public static bool IsMobile(string s) { if (string.IsNullOrEmpty(s)) { return false; } else { Regex r = new Regex(@"^\+?(86)?1(3[0-9]|47|5[0-9]|7[0-9]|8[0-9])\d{8}$"); return r.IsMatch(s); } } /// /// 判断是否为合法的MD5加密字串 /// public static bool IsMD5(string s) { if (string.IsNullOrEmpty(s)) { return false; } else { Regex r = new Regex(@"^[a-fA-F0-9]{32}$"); return r.IsMatch(s); } } /// /// 判断是否为合法的金额数字 /// public static bool IsMoney(string s) { if (string.IsNullOrEmpty(s)) { return false; } else { Regex r = new Regex(@"^(0(?:[.](?:[1-9]\d?|0[1-9]))|[1-9]\d*(?:[.]\d{1,2}|$))$"); return r.IsMatch(s); } } /// /// 字符串转换为日期类型数据 /// public static DateTime CDate(string date) { DateTime d; DateTime.TryParse(date, out d); if (d == DateTime.MinValue) { d = DateTime.Today; } return d; } /// /// 判断一个对象是否能够被转换为合法的日期格式,返回真或假 /// public static bool IsDate(object dt) { if (dt == null) { return false; } else if (dt is DateTime) { return true; } else if (dt is string) { DateTime d; return DateTime.TryParse((string)dt, out d); } else { return false; } } //截取指定长度字符串 public static string CutString(string str, int len) { if (str == null || str.Length == 0 || len <= 0) { return string.Empty; } int l = str.Length; int clen = 0; while (clen < len && clen < l) { //每遇到一个中文,则将目标长度减一。 if ((int)str[clen] > 128) { len--; } clen++; } if (clen < l) { return str.Substring(0, clen) + "..."; } else { return str; } } //页面跳转 public static void Redirect(string url) { HttpContext.Current.Response.Redirect(url, true); } //写入缓存 public static void InsertCache(string key, object value) { Cache.Insert(key, value); } //读出缓存 public static object GetCache(string key) { return Cache[key]; } //清除缓存 public static void RemoveCache(string key) { Cache.Remove(key); } //输出调试信息 public static void Debug(string msg) { HttpContext.Current.Response.Write(msg); HttpContext.Current.Response.End(); } //根据传入数字数组,计算最大值、最小值、平均分和标准差 public static float[] GetMaxandMinandMandSD(float[] Number) { float[] Result = { 0, 0, 0, 0 }; Result[0] = Number[0]; Result[1] = Number[0]; float Sum = 0; int Count = Number.Length; float SumSqdDevs = 0; //最大值、最小值、和 for (int i = 0; i < Count; i++) { if (Number[i] > Result[0]) { Result[0] = Number[i]; } if (Number[i] < Result[0]) { Result[1] = Number[i]; } Sum += Number[i]; } //平均分 Result[2] = Sum / Count; //方差 for (int i = 0; i < Count; i++) { SumSqdDevs += (float)Math.Pow((Number[i] - Result[2]), 2); } //标准差 Result[3] = (float)Math.Sqrt(SumSqdDevs / Count); return Result; } /// /// 判断字符串是否既不是Null也不是空值 /// public static bool IsNotNullAndEmpty(string s) { return !(s == null || s == string.Empty); } /// /// MD5加密(32位,小写) /// public static string MD5(string s) { return FormsAuthentication.HashPasswordForStoringInConfigFile(s, "MD5").ToLower(); } /// /// SHA1加密 /// public static string SHA1(string s) { return FormsAuthentication.HashPasswordForStoringInConfigFile(s, "SHA1").ToLower(); } //检查角色 public static void CheckRole(string role) { if (!HttpContext.Current.User.IsInRole(role)) { HttpContext.Current.Response.Redirect("Message.aspx?Msg=0对不起,您没有该项操作的权限!"); } } /// /// 替换字符串用于json时其中包含的转义字符 /// public static string JsonEncode(object json) { return json.ToString().Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\r", "\\r").Replace("\n", "\\n").Replace("\t", "\\t"); } /// /// 随机数字,传入数字位数 /// public static string RndNumber(int l) { StringBuilder sb = new StringBuilder(l); Random rd = new Random(); for (int i = 0; i < l; i++) { sb.Append(rd.Next(10)); } return sb.ToString(); } /// /// 从右边截取字符串 /// public static string Right(string str, int n) { return str.Substring(n > str.Length ? 0 : str.Length - n); } }