写在前面的
分享几个号码验证的工具类有需要的小伙伴们可以收藏~
验证电话号码
代码
/// <summary>
/// 验证电话号码
/// </summary>
/// <param name="str_telephone"></param>
/// <returns></returns>
public bool IsTelephone(string str_telephone)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_telephone,@"^(\d{3,4}-)?\d{6,8}$");
}
验证手机号码
代码
/// <summary>
/// 验证手机号码
/// </summary>
/// <param name="str_Mobile"></param>
/// <returns></returns>
public bool IsMobile(string str_Mobile)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_Mobile,@"^[1]+[3,5]+\d{9}");
}
验证身份证号码
代码一
/// <summary>
/// 验证身份证号码
/// </summary>
/// <param name="str_idcard"></param>
/// <returns></returns>
public bool IsIDcard(string str_idcard)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_idcard,@"(^\d{18}$)|(^\d{15}$)");
}
代码二
/// <summary>
/// 验证身份证号码
/// </summary>
/// <param name="str_idcard"></param>
/// <returns></returns>
public bool IsMail(string str_idmail)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_idmail, @"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");
}
判断数值
代码
/// <summary>
/// 判断数值
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public bool IsMatchString(string s) {
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("^((-{0,1}[0-9]+[\\.]?[0-9]+)|-{0,1}[0-9])$");
return r.IsMatch(s);
}