123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- namespace ETD.Data
- {
- using System;
- using System.Text.RegularExpressions;
- public class RegExp
- {
- /// <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>
- /// 检测是否符合email格式
- /// </summary>
- /// <param name="strEmail">要判断的email字符串</param>
- /// <returns>判断结果</returns>
- public static bool IsEmail(string strEmail)
- {
- return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
- }
- /// <summary>
- /// 判断是否是正常的ip格式
- /// </summary>
- /// <param name="ipAdress">验证字符串</param>
- /// <returns></returns>
- public static bool IsIP(string ipAdress)
- {
- return Regex.IsMatch(ipAdress, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
- }
- public static bool IsJsonSafety(string s)
- {
- string text1 = Regex.Replace(s.Replace("%20", " "), @"\s", " ");
- string text2 = @"select |insert |delete from |count\(|drop table|update |truncate |asc\(|mid\(|char\(|xp_cmdshell|exec master|net localgroup administrators|net user|\'| or ";
- return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase);
- }
- /// <summary>
- /// 判断给定的字符串(strNumber)是否是数值型
- /// </summary>
- /// <param name="strNumber">要确认的字符串</param>
- /// <returns>是则返加true 不是则返回 false</returns>
- public static bool IsNumber(string strNumber)
- {
- return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);
- }
- /// <summary>
- /// 判断是否是qq号码
- /// </summary>
- /// <param name="str">要验证的字符串</param>
- /// <returns></returns>
- public static bool IsQQ(string str)
- {
- return Regex.IsMatch(str, "[1-9][0-9]{4,}");
- }
- /// <summary>
- /// 检测是否有Sql危险字符
- /// </summary>
- /// <param name="str">要判断字符串</param>
- /// <returns>判断结果</returns>
- public static bool IsSafeSqlString(string str)
- {
- return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
- }
- public static bool IsSafety(string s)
- {
- string text1 = Regex.Replace(s.Replace("%20", " "), @"\s", " ");
- string text2 = "select |insert |delete from |count\\(|drop table|update |truncate |asc\\(|mid\\(|char\\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|\"|\\'| or ";
- return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase);
- }
- /// <summary>
- /// 判断是否是时间格式
- /// </summary>
- /// <param name="strTime">判断字符串</param>
- /// <returns></returns>
- public static bool IsTimeString(string strTime)
- {
- return Regex.IsMatch(strTime, "^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
- }
- /// <summary>
- /// 判断是否是小数
- /// </summary>
- /// <param name="str">要验证的字符串</param>
- /// <returns></returns>
- public static bool IsValidDecimal(string str)
- {
- return Regex.IsMatch(str, @"[0].\d{1,2}|[1]");
- }
- /// <summary>
- /// 判断是否是电话号码格式
- /// </summary>
- /// <param name="str">要验证的字符串</param>
- /// <returns></returns>
- public static bool IsValidTel(string str)
- {
- return Regex.IsMatch(str, @"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");
- }
- }
- }
|