常用方法
常用方法:
using System;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
namespace Util
{
public class CType
{
/// <summary>
/// 判断是否是整数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsInt(string value)
{
string pattern = @"^[+-]?\d+$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是小数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsDecimal(string value)
{
string pattern = @"^(\d*\.)?\d+$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 是否是实数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsRealNum(string value)
{
string pattern = @"^[-+]?[0-9]+\.?[0-9]*$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是合法的Email
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsEmail(string value)
{
string pattern = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是字母、数字和特殊字符(-_.'&)的组合
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsWord(string value)
{
string pattern = @"^([a-zA-z0-9_\-\.\'\&])*$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是合法的密码格式
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsPassword(string value)
{
string pattern = @"^(\w|\s|[`~!@#\$%\^&\*\(\)_\+\-=\{\}\[\]\:\'\<\>,\.\?\|/\\;""])*$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否为有效的用户名,包含英文字母、数字、和特殊字符('_-.&),特殊字符必须在字母或数字之后,并且只能在字串中间
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsUserName(string value)
{
string pattern = @"^(([a-zA-z0-9][\'_\-\.\&])*)?[a-zA-z0-9]+$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是中文、字母、数字和特殊字符(-_.'&)的组合
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsName(string value)
{
string pattern = @"^(\w|\s|[\'_\-\.\&])*$";
return Regex.IsMatch(value, pattern);
}
}
}
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
namespace Util
{
public class CType
{
/// <summary>
/// 判断是否是整数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsInt(string value)
{
string pattern = @"^[+-]?\d+$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是小数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsDecimal(string value)
{
string pattern = @"^(\d*\.)?\d+$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 是否是实数
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsRealNum(string value)
{
string pattern = @"^[-+]?[0-9]+\.?[0-9]*$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是合法的Email
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsEmail(string value)
{
string pattern = @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是字母、数字和特殊字符(-_.'&)的组合
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsWord(string value)
{
string pattern = @"^([a-zA-z0-9_\-\.\'\&])*$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是合法的密码格式
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsPassword(string value)
{
string pattern = @"^(\w|\s|[`~!@#\$%\^&\*\(\)_\+\-=\{\}\[\]\:\'\<\>,\.\?\|/\\;""])*$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否为有效的用户名,包含英文字母、数字、和特殊字符('_-.&),特殊字符必须在字母或数字之后,并且只能在字串中间
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsUserName(string value)
{
string pattern = @"^(([a-zA-z0-9][\'_\-\.\&])*)?[a-zA-z0-9]+$";
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 判断是否是中文、字母、数字和特殊字符(-_.'&)的组合
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsName(string value)
{
string pattern = @"^(\w|\s|[\'_\-\.\&])*$";
return Regex.IsMatch(value, pattern);
}
}
}