/// <summary>
///
/// </summary>
/// <param name="Sour"></param>
/// <returns></returns>
private static string CheckDate2(string Sour)
{
string text = "";
if (Sour == "")
{
return "日期不能为空";
}
if (Sour.CompareTo(DateTime.Now.ToString("yyyy-MM-dd")) < 0)
{
return "日期不能小于当前日期";
}
Match match = new Regex("^([0-9]{4})-([0-9]{2})-([0-9]{2})$").Match(Sour);
if (match.Success)
{
int year = Convert.ToInt32(match.Groups[1].Value);
int month = Convert.ToInt32(match.Groups[2].Value);
int day = Convert.ToInt16(match.Groups[3].Value);
try
{
new DateTime(year, month, day);
}
catch
{
text = "日期格式不正确,应为【2002-09-06】";
}
return text;
}
return "日期格式不正确,应为【2002-09-06】";
}
/// <summary>
/// 是否为数字,一组数字和对应的一组错误信息
/// </summary>
/// <param name="values">Values</param>
/// <param name="texts">错误信息</param>
/// <returns></returns>
public static string CheckDouble(string[] values, string[] texts)
{
string text = "";
int index = 0;
for (index = 0; index < values.Length; index++)
{
if (!IsDecimal(values[index]))
{
text = text + texts[index] + "必须为数字//n";
}
}
return text;
}
/// <summary>
/// 简体中文环境下检查日期格式是否正确 接受 8位或10位的字串,其它位皆为 false
/// </summary>
/// <param name="sour">20060514 or 2005-05-14 等 8位或10位日期字串</param>
/// <returns>Return Value</returns>
public static bool RegDateCHS(string sour)
{
string text = "";
if (sour.Length == 8)
{
text = sour.Substring(0, 4) + "-" + sour.Substring(4, 2) + "-" + sour.Substring(6, 2);
}
else if (sour.Length == 10)
{
text = sour.Substring(0, 4) + "-" + sour.Substring(5, 2) + "-" + sour.Substring(8, 2);
}
if (text == "")
{
return false;
}
try
{
Convert.ToDateTime(text);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
return RegDecimal.Match(inputData).Success;
}
/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
return RegDecimalSign.Match(inputData).Success;
}
/// <summary>
/// 比较日期,必须为10位 2002/09/15格式
/// </summary>
/// <param name="Sour">待测试的日期字串 </param>
/// <returns>ture or false</returns>
private static bool RegDate(string Sour)
{
Match match = new Regex(@"^([0-9]{4})//([0-9]{2})//([0-9]{2})$").Match(Sour);
bool flag = true;
if (match.Success)
{
int year = Convert.ToInt32(match.Groups[1].Value);
int month = Convert.ToInt32(match.Groups[2].Value);
int day = Convert.ToInt16(match.Groups[3].Value);
try
{
new DateTime(year, month, day);
}
catch
{
flag = false;
}
return flag;
}
return false;
}
/// <summary>
/// 如果为空,return false, else return true;
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static bool ToType(object key)
{
if (key == null)
{
return false;
}
if (key == DBNull.Value)
{
return false;
}
if (key.ToString() == string.Empty)
{
return false;
}
if (key.ToString().Trim() == "")
{
return false;
}
if (key.ToString().ToLower() == " ")
{
return false;
}
return true;
}
/// <summary>
/// 转换为String
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string ToString(object key)
{
if (ToType(key))
{
return key.ToString();
}
return "";
}
///
/// </summary>
/// <param name="Sour"></param>
/// <returns></returns>
private static string CheckDate2(string Sour)
{
string text = "";
if (Sour == "")
{
return "日期不能为空";
}
if (Sour.CompareTo(DateTime.Now.ToString("yyyy-MM-dd")) < 0)
{
return "日期不能小于当前日期";
}
Match match = new Regex("^([0-9]{4})-([0-9]{2})-([0-9]{2})$").Match(Sour);
if (match.Success)
{
int year = Convert.ToInt32(match.Groups[1].Value);
int month = Convert.ToInt32(match.Groups[2].Value);
int day = Convert.ToInt16(match.Groups[3].Value);
try
{
new DateTime(year, month, day);
}
catch
{
text = "日期格式不正确,应为【2002-09-06】";
}
return text;
}
return "日期格式不正确,应为【2002-09-06】";
}
/// <summary>
/// 是否为数字,一组数字和对应的一组错误信息
/// </summary>
/// <param name="values">Values</param>
/// <param name="texts">错误信息</param>
/// <returns></returns>
public static string CheckDouble(string[] values, string[] texts)
{
string text = "";
int index = 0;
for (index = 0; index < values.Length; index++)
{
if (!IsDecimal(values[index]))
{
text = text + texts[index] + "必须为数字//n";
}
}
return text;
}
/// <summary>
/// 简体中文环境下检查日期格式是否正确 接受 8位或10位的字串,其它位皆为 false
/// </summary>
/// <param name="sour">20060514 or 2005-05-14 等 8位或10位日期字串</param>
/// <returns>Return Value</returns>
public static bool RegDateCHS(string sour)
{
string text = "";
if (sour.Length == 8)
{
text = sour.Substring(0, 4) + "-" + sour.Substring(4, 2) + "-" + sour.Substring(6, 2);
}
else if (sour.Length == 10)
{
text = sour.Substring(0, 4) + "-" + sour.Substring(5, 2) + "-" + sour.Substring(8, 2);
}
if (text == "")
{
return false;
}
try
{
Convert.ToDateTime(text);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
return RegDecimal.Match(inputData).Success;
}
/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
return RegDecimalSign.Match(inputData).Success;
}
/// <summary>
/// 比较日期,必须为10位 2002/09/15格式
/// </summary>
/// <param name="Sour">待测试的日期字串 </param>
/// <returns>ture or false</returns>
private static bool RegDate(string Sour)
{
Match match = new Regex(@"^([0-9]{4})//([0-9]{2})//([0-9]{2})$").Match(Sour);
bool flag = true;
if (match.Success)
{
int year = Convert.ToInt32(match.Groups[1].Value);
int month = Convert.ToInt32(match.Groups[2].Value);
int day = Convert.ToInt16(match.Groups[3].Value);
try
{
new DateTime(year, month, day);
}
catch
{
flag = false;
}
return flag;
}
return false;
}
/// <summary>
/// 如果为空,return false, else return true;
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static bool ToType(object key)
{
if (key == null)
{
return false;
}
if (key == DBNull.Value)
{
return false;
}
if (key.ToString() == string.Empty)
{
return false;
}
if (key.ToString().Trim() == "")
{
return false;
}
if (key.ToString().ToLower() == " ")
{
return false;
}
return true;
}
/// <summary>
/// 转换为String
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string ToString(object key)
{
if (ToType(key))
{
return key.ToString();
}
return "";
}