Validate日期校验

这个博客分享了关于C#和Java中进行日期验证的方法,包括正则表达式和自定义验证函数,用于检查各种格式的有效性,如身份证、邮箱、电话号码等。此外,还介绍了日期和时间戳之间的转换方法。
摘要由CSDN通过智能技术生成

public class Validate
{
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
private static Regex RegEmail = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");

#region 用户名密码格式

/// <summary>
/// 返回字符串真实长度, 1个汉字长度为2
/// </summary>
/// <returns>字符长度</returns>
public static int GetStringLength(string stringValue)
{
return Encoding.Default.GetBytes(stringValue).Length;
}

/// <summary>
/// 检测用户名格式是否有效
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static bool IsValidUserName(string userName)
{
int userNameLength = GetStringLength(userName);
if (userNameLength >= 4 && userNameLength <= 20 && Regex.IsMatch(userName, @"^([\u4e00-\u9fa5A-Za-z_0-9]{0,})$"))
{ // 判断用户名的长度(4-20个字符)及内容(只能是汉字、字母、下划线、数字)是否合法
return true;
}
else
{
return false;
}
}

/// <summary>
/// 密码有效性
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static bool IsValidPassword(string password)
{
return Regex.IsMatch(password, @"^[A-Za-z_0-9]{6,16}$");
}
#endregion

#region 数字字符串检查

/// <summary>
/// int有效性
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
static public bool IsValidInt(string val)
{
return Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
}

/// <summary>
/// 是否数字字符串
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumber(string inputData)
{
Match m = RegNumber.Match(inputData);
return m.Success;
}

/// <summary>
/// 是否数字字符串 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
}

/// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimalPrecise(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
}

/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
try {
Convert.ToDecimal(inputData);
return true;
}
catch
{
return false;
}
}

/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool Isint(string inputData)
{
try
{
Convert.ToInt64(inputData);
return true;
}
catch
{
return false;
}
}


/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecima

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值