using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CP.Common { public static class ValidationTools { /// /// 验证int数据并且比较最大最小超出范围取最小 /// /// value /// 最小值 /// 最大值 /// public static int CheckInt(Object value, int min, int max) { try { int tmp = 0; int.TryParse(value.ToString(), out tmp); if (tmp > max || tmp < min) { tmp = min; } return tmp; } catch (Exception ee) { return 0; } } /// /// 验证字符串是否有非法字符 如果是将返回默认 /// /// value /// 默认字符串 /// 最大长度 /// public static string CheckStr(this string value, string defaultStr, int maxlength) { try { string[] aryReg = { "'", "'delete", "?", "<", ">", "%", "\"\"", ",", ".", ">=", "=<", ";", "||", "[", "]", "&", "/", "|", " ", "''" }; string tmp = defaultStr; if (value.Length < maxlength) { var regchar = value.Where(w => aryReg.ToList().Contains(w.ToString())).ToList(); if (regchar.Count != 0) { tmp = defaultStr; } else { tmp = value; } } return tmp; } catch (Exception ee) { return null; } } /// /// 判断字符串是否包含其中 /// /// value /// params参数 /// public static bool CheckHasChar(string value, params string[] charStr) { try { int istrue = 0; for (int i = 0; i < charStr.Length; i++) { if (value.Contains(charStr[i])) { istrue += 1; } } if (istrue == charStr.Length) { return true; } else { return false; } } catch (Exception ee) { return false; } } /// ///验证字符串是否是符合标准的时间格式数据 /// /// value /// 默认返回时间 /// 最小时间 /// 最大时间 /// public static DateTime CheckTime(string value, DateTime defaultTime, DateTime mintime, DateTime maxTime) { try { DateTime tmp = DateTime.Now; DateTime.TryParse(value, out tmp); if (tmp > maxTime || tmp < mintime) { tmp = defaultTime; } return tmp; } catch (Exception ee) { return defaultTime; } } } }