tnblog
首页
视频
资源
登录

验证合法性扩展方法

3155人阅读 2020/3/31 15:08 总访问:347642 评论:0 收藏:0 手机
分类: ASP.NET
Email 格式是否合法
public static bool IsEmail(this string source)
{
    return Regex.IsMatch(source, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
}
Money 格式是否合法
public static bool IsMoney(this string source)
 {
       return Regex.IsMatch(source, @"^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{1,}$");
 }
带小数点数字
public static bool IsDoubleNumber(this string input)
 {
        string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
        Regex regex = new Regex(pattern);
        return regex.IsMatch(input);
}
数字 格式是否合法
public static bool IsNumber(this string source)
 {
        return Regex.IsMatch(source, @"^[0-9]*$");
 }
判断是否IP
public static bool IsIp(this string source)
 {
        return Regex.IsMatch(source,@"^(((25[0-5]|2[0-4][0-9]|19[0-1]|19[3-9]|18[0-9]|17[0-1]|17[3-9]|1[0-6][0-9]|1[1-9]|[2-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|(192\.(25[0-5]|2[0-4][0-9]|16[0-7]|169|1[0-5][0-9]|1[7-9][0-9]|[1-9][0-9]|[0-9]))|(172\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|1[0-5]|3[2-9]|[4-9][0-9]|[0-9])))\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$");
}
检查字符串是否为A-Z、0-9及下划线以内的字符
public static bool IsLetterOrNumber(this string source)
 {
      bool b = System.Text.RegularExpressions.Regex.IsMatch(source, @"\w");
      return b;
}
验输入字符串是否含有“/\:.?*|$]”特殊字符
public static bool IsSpecialChar(this string source)
{
      Regex r = new Regex(@"[/\<>:.?*|$]");
      return r.IsMatch(source);
}
是否全为中文/日文/韩文字符
public static bool IsChineseChar(this string source)
        {
            //中文/日文/韩文: [\u4E00-\u9FA5]
            //英文:[a-zA-Z]
            return Regex.IsMatch(source, @"^[\u4E00-\u9FA5]+$");
        }
是否为日期型字符串
 public static bool IsDate(this string source)
  {
            return Regex.IsMatch(source,
                @"^((((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-))$");
 }
是否包含双字节字符(允许有单字节字符)
public static bool IsDoubleChar(this string source)
        {
            return Regex.IsMatch(source, @"[^\x00-\xff]");
        }
是否为时间型字符串,时间字符串(15:00:00)
public static bool IsTime(this string source)
        {
            return Regex.IsMatch(source, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }
是否为日期+时间型字符串
public static bool IsDateTime(this string source)
        {
            return Regex.IsMatch(source,
                @"^(((((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-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }




评价
当你知道迷惑时,并不可怜,当你不知道迷惑时,才是最可怜的。
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术