/*
* 版权所有:Copyright (C) 2013,河南华谊科技
* Version: 1.0
* 创建日期: 2013-05-16
* 作者: 魏飞
* 说明: 常用正则验证
* 修改日期: yyyy-MM-dd
* 修改者: 修改者
* 修改说明: 修改说明
*/
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace HySoft.Common
{
///
/// 提供常用正则验证
///
public class RegexUtil
{
///
/// 是否为纯数字
///
///
///
public static bool IsNumber(string str)
{
return Regex.IsMatch(str, @"^\d+$");
}
///
/// 是否为纯字母
///
///
///
public static bool IsLetter(string str)
{
return Regex.IsMatch(str, @"^[a-zA-Z]+$");
}
///
/// 是否为图片格式
///
///
///
public static bool IsImage(string filename)
{
return Regex.IsMatch(filename, @"(?i)\.(jpg|gif|png|bmp|tiff)$");
}
///
/// 验证是否为用户名格式(字母、数字、下划线、汉字)
///
///
///
public static bool IsUsername(string str)
{
return Regex.IsMatch(str, @"^\w+$");
}
public static bool IsUrl(string str)
{
return Regex.IsMatch(str, @"(?i)^http(?s)://([\w\-]+\.)+[\w\-]+(/[\w\-./?%&=]*)?$");
}
public static bool IsEmail(string str)
{
return Regex.IsMatch(str, @"(?i)^([a-z0-9][a-z0-9_\-\.]+)@([a-z0-9][a-z0-9\.\-]{0,20})\.([a-z]{2,4})$");
}
public static bool IsIpAddress(string str)
{
return Regex.IsMatch(str, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
}
///
/// 验证是否为身份证号
///
///
///
public static bool IsIDCardNumber(string str)
{
if (str.Length == 15)
{
return Regex.IsMatch(str, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
}
else if (str.Length == 18)
{
return Regex.IsMatch(str, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$");
}
return false;
}
///
/// 是否为手机号
///
///
///
public static bool IsMobile(string str)
{
return Regex.IsMatch(str, @"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$");
}
///
/// 是否为电话号
///
///
///
public static bool IsTelphone(string str)
{
string patt = @"^((\d{7,8})|(\d{3,4})(-|\s)?(\d{7,8})|(\d{3,4})(-|\s)?(\d{7,8})(-|\s)(\d{1,4})|(\d{7,8})(-|\s)(\d{1,4}))$";
return Regex.IsMatch(str, patt);
}
///
/// 是否为日期格式(不含时间)
///
///
///
public static bool IsDate(string str)
{
string patt = @"^(?:(?!0000)[0-9]{4}([-/.\s]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.\s]?)0?2\2(?:29))$";
return Regex.IsMatch(str, patt);
}
///
/// 是否为时间格式
///
///
///
public static bool IsTime(string str)
{
return Regex.IsMatch(str, @"^([01]?[0-9]|2?[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$");
}
///
/// 是否为日期时间格式
///
///
///
public static bool IsDateTime(string str)
{
string patt = @"^(?:(?!0000)[0-9]{4}([-/.\s]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-/.\s]?)0?2\2(?:29))\s+([01]?[0-9]|2?[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$";
return Regex.IsMatch(str, patt);
}
///
/// 验证是否为用户名格式(字母、数字、下划线)
///
///
///
public static bool IsUserName(string str)
{
return Regex.IsMatch(str, @"^[0-9a-zA-Z_]+$");
}
///
/// 验证是否为字母数字格式(字母、数字)
///
///
///
public static bool IsOnlyLetterNumber(string str)
{
return Regex.IsMatch(str, @"^[0-9a-zA-Z]+$");
}
///
/// 是否为手机号或电话
///
///
///
public static bool IsMobileOrTel(string str)
{
return Regex.IsMatch(str, @"^((\d{3,4}-\d{7,8})|(\d{11}))$");
}
///
/// 是否为11位数字
///
///
///
public static bool IsMobile1(string str)
{
return Regex.IsMatch(str, @"^(\d{11})$");
}
///
/// 是否为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?)$");
}
}
}