using System; using System.Text.RegularExpressions; namespace Zhaizj.Framework.Utils { /// /// 关于参数验证的一些实用方法 /// public static class Checker { /// /// 检查字符串是否为空(null或者长度为0) /// /// 字符串名 /// 被检查的字符串 /// 为空时是否抛出异常 /// 为空则返回true public static bool CheckEmptyString(string argName, string argValue, bool throwError) { CheckArgumentNull("argName", argName, true); bool ret = (argValue == null || argValue.Length == 0); if(ret && throwError) { throw new ArgumentException("字符串为空", argName); } return ret; } /// /// 检查参数是否为空引用(null) /// /// 参数名 /// 被检查的参数 /// 为空引用时是否抛出异常 /// 为空则返回true public static bool CheckArgumentNull(string argName, object argValue, bool throwError) { if(argName == null) { throw new ArgumentNullException("argName"); } if(argValue == null) { if(throwError) { throw new ArgumentNullException(argName); } else { return true; } } return false; } /// /// 检查数组是否为空(长度为0) /// /// 数组名 /// 被检查的数组实例 /// 为空引用时是否抛出异常 /// 为空则返回true public static bool CheckEmptyArray(string argName, Array argValue, bool throwError) { if(argName == null) { throw new ArgumentNullException("argName"); } bool ret = (argValue == null || argValue.Length == 0); if(ret && throwError) { throw new ArgumentException("数组为空", argName); } return ret; } /// /// 判断某值是否在枚举内(位枚举) /// /// 被检测的枚举值 /// 期望的枚举值 /// 是否包含 public static bool CheckFlagsEnumEquals(Enum checkingValue, Enum expectedValue) { int intCheckingValue = Convert.ToInt32(checkingValue); int intExpectedValue = Convert.ToInt32(expectedValue); return (intCheckingValue & intExpectedValue) == intExpectedValue; } /// /// 判断是否为base64字符串 /// /// /// public static bool IsBase64String(string str) { //A-Z, a-z, 0-9, +, /, = return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]"); } /// /// 检测是否有Sql危险字符 /// /// 要判断字符串 /// 判断结果 public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"); } /// /// 判断文件名是否为浏览器可以直接显示的图片文件名 /// /// 文件名 /// 是否可以直接显示 public static bool IsImgFilename(string filename) { filename = filename.Trim(); if (filename.EndsWith(".") || filename.IndexOf(".") == -1) { return false; } string extname = filename.Substring(filename.LastIndexOf(".") + 1).ToLower(); return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif"); } /// /// 是否为ip /// /// /// public static bool IsIP(string ip) { return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } public static bool IsIPSect(string ip) { return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){2}((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$"); } /// /// 返回指定IP是否在指定的IP数组所限定的范围内, IP数组内的IP地址可以使用*表示该IP段任意, 例如192.168.1.* /// /// /// /// public static bool InIPArray(string ip, string[] iparray) { string[] userip = StringHelper.SplitString(ip, @"."); for (int ipIndex = 0; ipIndex < iparray.Length; ipIndex++) { string[] tmpip = StringHelper.SplitString(iparray[ipIndex], @"."); int r = 0; for (int i = 0; i < tmpip.Length; i++) { if (tmpip[i] == "*") { return true; } if (userip.Length > i) { if (tmpip[i] == userip[i]) { r++; } else { break; } } else { break; } } if (r == 4) { return true; } } return false; } public static bool IsDateFormat(string date) { return Regex.IsMatch(date, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"); } } }