/********************************************** * 类作用: 验证实用类 * 作者:开优网络 * http://www.kaiu.net ***********************************************/ using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Text.RegularExpressions; namespace Svnhost.Common { public class ValidateUtils { #region 数据类型验证 /// <summary> /// 判断是否为base64字符串 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsBase64String(string str) { //A-Z, a-z, 0-9, +, /, = return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]"); } /// <summary> /// 验证是否为正整数 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsInt(string str) { return Regex.IsMatch(str, @"^[0-9]*$"); } /// <summary> /// 判断对象是否为Int32类型的数字 /// </summary> /// <param name="Expression"></param> /// <returns></returns> public static bool IsNumeric(object Expression) { if (Expression != null) { string str = Expression.ToString(); if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$")) { if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1')) { return true; } } } return false; } public static bool IsDouble(object Expression) { if (Expression != null) { return Regex.IsMatch(Expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$"); } return false; } /// <summary> /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型 /// </summary> /// <param name="strNumber">要确认的字符串数组</param> /// <returns>是则返加true 不是则返回 false</returns> public static bool IsNumericArray(string[] strNumber) { if (strNumber == null) { return false; } if (strNumber.Length < 1) { return false; } foreach (string id in strNumber) { if (!IsNumeric(id)) { return false; } } return true; } #endregion #region 字符串功能验证 /// <summary> /// 是否包涵制定的字符串 /// </summary> /// <param name="str"></param> /// <param name="stringarray"></param> /// <param name="strsplit"></param> /// <returns></returns> public static bool IsCompriseStr(string str, string stringarray, string strsplit) { if (stringarray == "" || stringarray == null) { return false; } str = str.ToLower(); string[] stringArray = StringUtil.SplitString(stringarray.ToLower(), strsplit); for (int i = 0; i < stringArray.Length; i++) { if (str.IndexOf(stringArray[i]) > -1) { return true; } } return false; } /// <summary> /// 判断指定字符串是否属于指定字符串数组中的一个元素 /// </summary> /// <param name="strSearch">字符串</param> /// <param name="stringArray">字符串数组</param> /// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param> /// <returns>判断结果</returns> public static bool InArray(string strSearch, string[] stringArray, bool caseInsensetive) { return StringUtil.GetInArrayID(strSearch, stringArray, caseInsensetive) >= 0; } /// <summary> /// 判断指定字符串是否属于指定字符串数组中的一个元素 /// </summary> /// <param name="str">字符串</param> /// <param name="stringarray">字符串数组</param> /// <returns>判断结果</returns> public static bool InArray(string str, string[] stringarray) { return InArray(str, stringarray, false); } /// <summary> /// 判断指定字符串是否属于指定字符串数组中的一个元素 /// </summary> /// <param name="str">字符串</param> /// <param name="stringarray">内部以逗号分割单词的字符串</param> /// <returns>判断结果</returns> public static bool InArray(string str, string stringarray) { return InArray(str, StringUtil.SplitString(stringarray, ","), false); } /// <summary> /// 判断指定字符串是否属于指定字符串数组中的一个元素 /// </summary> /// <param name="str">字符串</param> /// <param name="stringarray">内部以逗号分割单词的字符串</param> /// <param name="strsplit">分割字符串</param> /// <returns>判断结果</returns> public static bool InArray(string str, string stringarray, string strsplit) { return InArray(str, StringUtil.SplitString(stringarray, strsplit), false); } /// <summary> /// 判断指定字符串是否属于指定字符串数组中的一个元素 /// </summary> /// <param name="str">字符串</param> /// <param name="stringarray">内部以逗号分割单词的字符串</param> /// <param name="strsplit">分割字符串</param> /// <param name="caseInsensetive">是否不区分大小写, true为不区分, false为区分</param> /// <returns>判断结果</returns> public static bool InArray(string str, string stringarray, string strsplit, bool caseInsensetive) { return InArray(str, StringUtil.SplitString(stringarray, strsplit), caseInsensetive); } /// <summary>是否空</summary> /// <param name="strInput">输入字符串</param> /// <returns>true/false</returns> public static bool isBlank(string strInput) { if (strInput == null || strInput.Trim() == "") { return true; } else { return false; } } #endregion #region IP地址 SQL注入等安全验证 /// <summary> /// 是否为ip /// </summary> /// <param name="ip"></param> /// <returns></returns> 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?|\*)$"); } /// <summary> /// 检测是否有Sql危险字符 /// </summary> /// <param name="str">要判断字符串</param> /// <returns>判断结果</returns> public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"); } /// <summary> /// 检测是否有危险的可能用于链接的字符串 /// </summary> /// <param name="str">要判断字符串</param> /// <returns>判断结果</returns> public static bool IsSafeUserInfoString(string str) { return !Regex.IsMatch(str, @"^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|游客|^Guest"); } /// <summary> /// 返回指定IP是否在指定的IP数组所限定的范围内, IP数组内的IP地址可以使用*表示该IP段任意, 例如192.168.1.* /// </summary> /// <param name="ip"></param> /// <param name="iparray"></param> /// <returns></returns> public static bool InIPArray(string ip, string[] iparray) { string[] userip = StringUtil.SplitString(ip, @"."); for (int ipIndex = 0; ipIndex < iparray.Length; ipIndex++) { string[] tmpip = StringUtil.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; } #endregion #region 其他功能验证 /// <summary> /// 判断文件流是否为UTF8字符集 /// </summary> /// <param name="sbInputStream">文件流</param> /// <returns>判断结果</returns> private static bool IsUTF8(FileStream sbInputStream) { int i; byte cOctets; // octets to go in this UTF-8 encoded character byte chr; bool bAllAscii = true; long iLen = sbInputStream.Length; cOctets = 0; for (i = 0; i < iLen; i++) { chr = (byte)sbInputStream.ReadByte(); if ((chr & 0x80) != 0) bAllAscii = false; if (cOctets == 0) { if (chr >= 0x80) { do { chr <<= 1; cOctets++; } while ((chr & 0x80) != 0); cOctets--; if (cOctets == 0) return false; } } else { if ((chr & 0xC0) != 0x80) { return false; } cOctets--; } } if (cOctets > 0) { return false; } if (bAllAscii) { return false; } return true; } #endregion #region 日期类型格式验证 /// <summary> /// /// </summary> /// <returns></returns> public static bool IsTime(string timeval) { return Regex.IsMatch(timeval, @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$"); } /// <summary> /// 判断字符串是否是yy-mm-dd字符串 /// </summary> /// <param name="str">待判断字符串</param> /// <returns>判断结果</returns> public static bool IsDateString(string str) { return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})"); } /// <summary>是否日期</summary> /// <param name="strInput">输入字符串</param> /// <returns>true/false</returns> public static bool isDate(string strInput) { string datestr = strInput; string year, month, day; string[] c ={ "/", "-", "." }; string cs = ""; for (int i = 0; i < c.Length; i++) { if (datestr.IndexOf(c[i]) > 0) { cs = c[i]; break; } }; if (cs != "") { year = datestr.Substring(0, datestr.IndexOf(cs)); if (year.Length != 4) { return false; }; datestr = datestr.Substring(datestr.IndexOf(cs) + 1); month = datestr.Substring(0, datestr.IndexOf(cs)); if ((month.Length != 2) || (Convert.ToInt16(month) > 12)) { return false; }; datestr = datestr.Substring(datestr.IndexOf(cs) + 1); day = datestr; if ((day.Length != 2) || (Convert.ToInt16(day) > 31)) { return false; }; return checkDatePart(year, month, day); } else { return false; } } /// <summary> /// 检查年月日是否合法 /// </summary> /// <param name="dt"></param> /// <param name="part"></param> /// <returns></returns> private static bool checkDatePart(string year, string month, string day) { int iyear = Convert.ToInt16(year); int imonth = Convert.ToInt16(month); int iday = Convert.ToInt16(day); if (iyear > 2099 || iyear < 1900) { return false; } if (imonth > 12 || imonth < 1) { return false; } if (iday > DateUtil.GetDaysOfMonth(iyear, imonth) || iday < 1) { return false; }; return true; } #endregion } }