首页 > 代码库 > 字符串验证类
字符串验证类
/***************************************************** * 文件名:StringValidation.cs * 功能描述:扩展方法:字符串验证 * 创建时间:2014-6-7 * 作 者: Eric * * 修改时间: * 修改人: * 修改描述 * ******************************************************/ public static class StringValidation { /// <summary> /// 验证IP地址 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsIP(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", RegexOptions.IgnoreCase); } /// <summary> /// 验证电话号 如:010-29292929 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsTelephone(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase); } /// <summary> /// 验证手机号 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsMobilePhone(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^1[35]\d{9}$", RegexOptions.IgnoreCase); } /// <summary> /// 验证邮箱地址 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsEmail(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", RegexOptions.IgnoreCase); ; } /// <summary> /// 验证日期数据 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsDate(this string str) { try { DateTime time = Convert.ToDateTime(str); return true; } catch { return false; } } /// <summary> /// 验证是否是中文 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsChinese(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase); } /// <summary> /// 验证URL /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsUrl(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?$", RegexOptions.IgnoreCase); } /// <summary> /// 验证邮编 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsZipCode(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, @"^\d{6}$", RegexOptions.IgnoreCase); } /// <summary> /// 验证QQ /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsQQ(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, "^[1-9]\\d{4,9}$"); } /// <summary> /// 验证纯字符 /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsLetter(this string str) { return string.IsNullOrEmpty(str) && Regex.IsMatch(str, "^[a-zA-Z]+$"); } /// <summary> /// 验证字符串长度 /// </summary> /// <param name="str"></param> /// <param name="begin"></param> /// <param name="end"></param> /// <returns></returns> public static bool IsLength(string str, int begin, int end) { int length = Regex.Replace(str, @"[^\x00-\xff]", "OK").Length; if ((length <= begin) && (length >= end)) { return false; } return true; } //// <summary> /// 是不是Int型的 /// </summary> /// <param name="source"></param> /// <returns></returns> public static bool IsInt(string str) { Regex regex = new Regex(@"^(-){0,1}\d+$"); if (regex.Match(str).Success) { if ((long.Parse(str) > 0x7fffffffL) || (long.Parse(str) < -2147483648L)) { return false; } return true; } return false; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。