#region // Fields
private static Regex RegDecimal;
private static Regex RegCHZN;
private static Regex RegDecimalSign;
private static Regex RegEmail;
private static Regex RegNumber;
private static Regex RegNumberSign;
#endregion
#region // Methods
/// <summary>
/// 构造函数
/// </summary>
public CheckData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
static CheckData()
{
RegNumber = new Regex("^[0-9]+$");
RegNumberSign = new Regex("^[+-]?[0-9]+$");
RegDecimal = new Regex("^[0-9]+[.]?[0-9]*$");
RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]*$");
RegEmail = new Regex(@"^[/w-]+@[/w-]+/.(com|cn|net|org|edu|mil|tv|biz|info)$");
RegCHZN = new Regex("[/u4e00-/u9fa5]");
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息适用于 2006/06/13 格式
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <returns></returns>
public static string CheckDate(string Sour)
{
return CheckDate(Sour, true, true);
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <param name="IsUpToday">是否必须大于今天</param>
/// <param name="IsNotNull">是否不可以为空值</param>
/// <returns></returns>
public static string CheckDate(string Sour, bool IsUpToday, bool IsNotNull)
{
if (IsNotNull)
{
if (Sour == "")
{
return "日期不能为空";
}
}
else if (Sour == "")
{
return "";
}
if (IsUpToday && (Sour.CompareTo(DateTime.Now.ToString(@"yyyy//MM//dd")) < 0))
{
return "日期不能小于当前日期";
}
if (!RegDate(Sour))
{
return "日期格式不正确,应为【2002/09/06】";
}
return "";
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <param name="IsUpToday">是否必须大于今天</param>
/// <returns></returns>
public static string CheckDate(string Sour, bool IsUpToday)
{
return CheckDate(Sour, IsUpToday, true);
}
private static Regex RegDecimal;
private static Regex RegCHZN;
private static Regex RegDecimalSign;
private static Regex RegEmail;
private static Regex RegNumber;
private static Regex RegNumberSign;
#endregion
#region // Methods
/// <summary>
/// 构造函数
/// </summary>
public CheckData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
static CheckData()
{
RegNumber = new Regex("^[0-9]+$");
RegNumberSign = new Regex("^[+-]?[0-9]+$");
RegDecimal = new Regex("^[0-9]+[.]?[0-9]*$");
RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]*$");
RegEmail = new Regex(@"^[/w-]+@[/w-]+/.(com|cn|net|org|edu|mil|tv|biz|info)$");
RegCHZN = new Regex("[/u4e00-/u9fa5]");
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息适用于 2006/06/13 格式
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <returns></returns>
public static string CheckDate(string Sour)
{
return CheckDate(Sour, true, true);
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <param name="IsUpToday">是否必须大于今天</param>
/// <param name="IsNotNull">是否不可以为空值</param>
/// <returns></returns>
public static string CheckDate(string Sour, bool IsUpToday, bool IsNotNull)
{
if (IsNotNull)
{
if (Sour == "")
{
return "日期不能为空";
}
}
else if (Sour == "")
{
return "";
}
if (IsUpToday && (Sour.CompareTo(DateTime.Now.ToString(@"yyyy//MM//dd")) < 0))
{
return "日期不能小于当前日期";
}
if (!RegDate(Sour))
{
return "日期格式不正确,应为【2002/09/06】";
}
return "";
}
/// <summary>
/// 如果格式正确则返回空,否则返回错误信息
/// </summary>
/// <param name="Sour">待测试的日期字串</param>
/// <param name="IsUpToday">是否必须大于今天</param>
/// <returns></returns>
public static string CheckDate(string Sour, bool IsUpToday)
{
return CheckDate(Sour, IsUpToday, true);
}
本文介绍了一个用于验证各种格式输入的实用类CheckData,包括整数、小数、带符号数字、电子邮件地址及中文字符等。通过静态构造函数初始化正则表达式,提供了检查日期格式的方法,确保日期格式符合特定标准且可根据需求验证日期是否为空或是否晚于当前日期。
2948

被折叠的 条评论
为什么被折叠?



