asp.net中常用信息验证总结

/// <summary>
/// 判断字符串是否是字母数字下划线组成
/// </summary>
public static bool IsNumWord(string inString)
{
Regex regex = new Regex("^[0-9a-zA-Z_]{1,}$");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是正的浮点数、是否是零
/// </summary>
public static bool IsPositiveFloatAndZero(string inString)
{
Regex regex = new Regex("^\\d+(\\.\\d+)?$");
return regex.IsMatch(inString.Trim());
}

/// <summary>
/// 判断字符串是否是正的浮点数
/// </summary>
private static bool IsPositive(string inString)
{
Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是日期格式“年-月-日”
/// </summary>
public static bool IsDate(string inString)
{
Regex regex = new Regex("^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$");
return regex.IsMatch(inString.Trim());
}

/// <summary>
/// 判断字符串是否是正整数
/// </summary>
public static bool IsInt(string inString)
{
Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是中文
/// </summary>
public static bool IsChinese(string inString)
{
Regex regex = new Regex("[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是电话号码
/// </summary>
public static bool IsTel(string inString)
{
Regex regex = new Regex(@"(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[358]\d{9})$)");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是手机号码
/// </summary>
public static bool IsMobilePhone(string inString)
{
Regex regex = new Regex(@"(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[358]\d{9})$)");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是Email
/// </summary>
public static bool IsEmail(string inString)
{
Regex regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是邮政编码
/// </summary>
public static bool IsPostalcode(string inString)
{
Regex regex = new Regex("^\\d{6}$");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是身份证码
/// </summary>
public static bool IsNumberCard(string inString)
{
Regex regex = new Regex(@"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是网址
/// </summary>
public static bool IsIntnetUrl(string inString)
{
Regex regex = new Regex("^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$");
return regex.IsMatch(inString.Trim());
}
/// <summary>
/// 判断字符串是否是IP
/// </summary>
public static bool IsIP(string inString)
{
Regex regex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
return regex.IsMatch(inString.Trim());
//Regex regex = new Regex(@"\b(((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])(\b|\.)){4}");
//return regex.IsMatch(inString.Trim());
}

 

posted @ 2012-03-13 11:32  一毛钱的爱  阅读(209)  评论(0编辑  收藏  举报