c# 字符串验证(邮箱、电话、数字、ip、身份证等)
using System;
using System.Text.RegularExpressions; namespace HuaTong.General.Utility
{
/// <summary>
/// 字符串验证
/// </summary>
public static class StringValidate
{
private static Regex RegNumeric = new Regex("^[0-9]+$");
private static Regex RegNumericSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^([0-9]+[.]?[0-9]+)|([0-9]+)$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");
private static Regex RegEmail = new Regex(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
private static Regex RegChina = new Regex("^[\u4e00-\u9fa5]*$");
private static Regex RegPostcode = new Regex(@"^(\d{6})$");
private static Regex RegUrl = new Regex(@"^(http|https|ftp|mms)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$");
private static Regex RegTEL = new Regex(@"^\d{6,8}|\d{3,4}-\d{6,8}$");
private static Regex RegDate = new Regex(@"^\d{4}(\-|\/|\.)\d{1,2}(\-|\/|\.)\d{1,2}$");
private static Regex RegMobile = new Regex(@"^1(3|4|5|7|8|9)\d{9}$");
private static Regex RegUserName = new Regex("^([\u4E00-\u9FA5a-zA-Z0-9_-]){2,16}$");
private static Regex RegIP = new Regex(@"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
private static Regex RegIdCard = new Regex(@"^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65|71|81|82|91)(\d{13}|\d{15}[\dxX])$"); /// <summary>
/// 自定义验证
/// </summary>
public static bool IsMatch(string value, string partten)
{
if (!string.IsNullOrEmpty(value))
{
Regex reg = new Regex(partten);
Match m = reg.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否数字
/// </summary>
public static bool IsNumeric(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegNumeric.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否数字 带正负号
/// </summary>
public static bool IsNumberSign(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegNumericSign.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否是小数
/// </summary>
public static bool IsDecimal(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegDecimal.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否是小数 带正负号
/// </summary>
public static bool IsDecimalSign(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegDecimalSign.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否中文
/// </summary>
public static bool IsHasCHZN(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegChina.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否是Email
/// </summary>
public static bool IsEmail(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegEmail.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否是邮政编码
/// </summary>
public static bool IsPostcode(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegPostcode.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否是URL
/// </summary>
public static bool IsUrl(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegUrl.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否日期
/// </summary> /// <returns></returns>
public static bool IsDate(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegDate.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否日期时间
/// </summary>
public static bool IsDatetime(string value)
{
DateTime time;
return DateTime.TryParse(value, out time);
} /// <summary>
/// 是否为合法的电话号码
/// </summary>
public static bool IsTel(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegTEL.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否为合法的手机号码
/// </summary>
public static bool IsMobile(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegMobile.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否为合法的用户名 限中文/英文/数字/减号/下划线
/// </summary>
public static bool IsUserName(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegUserName.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 是否为合法的IPv4地址
/// </summary>
public static bool IsIPAddress(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegIP.Match(value);
return m.Success;
}
else
{
return false;
}
} /// <summary>
/// 判断对象是否为空
/// </summary>
public static bool IsNullOrEmpty(object value)
{
if (value == null)
{
return true;
}
if (value.GetType() == typeof(String))
{
if (string.IsNullOrEmpty(value.ToString().Trim()))
{
return true;
}
}
if (value.GetType() == typeof(DBNull))
{
return true;
} //不为空
return false;
} /// <summary>
/// 是否合法身份证号
/// </summary>
public static bool IsIdCard(string value)
{
if (!string.IsNullOrEmpty(value))
{
Match m = RegIdCard.Match(value);
return m.Success;
}
else
{
return false;
}
}
}
}
c# 字符串验证(邮箱、电话、数字、ip、身份证等)的更多相关文章
- C#验证邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP类等常用函数封装
#region 验证邮箱验证邮箱 /**//// <summary> /// 验证邮箱 /// </summary> /// <param name="sour ...
- php使用过滤器filter_var轻松验证邮箱url和ip地址等
以前使用php的时候还不知道有过滤器filter这玩意,那时候判断邮箱.url和ip地址格式是否符合都是用正则表达式.后来随着使用的逐渐深入,才知道在php中也可以使用内置的函数库过滤器filter来 ...
- web开发常用的js验证,利用正则表达式验证邮箱、手机、身份证等输入
正则表达式验证 //邮箱 \-])+\.)+([a-zA-Z0-]{,})+$/; email = document.getElementById("email").value; ...
- C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP (转)
namespace YongFa365.Validator { using System; using System.Text.RegularExpressions; /**//// <summ ...
- 验证中文、英文、电话、手机、邮箱、数字、数字和字母、Url地址和Ip地址的正则表达式
Helper类代码 public class Helper { #region 单列循环 private Helper() { } private static Helper instance = n ...
- php自带验证邮箱 url ip函数
以前用PHP验证邮箱.URL.IP是否合法都是通过自己写正则来实现,但是有时候脑子发昏,可能会写出一个不是完全正确的正则,导致验证出错,今天发现原来PHP本身自带了验证邮箱.URL.IP是否合法的函数 ...
- PHP自带方法验证邮箱、URL、IP是否合法
PHP验证邮箱.URL.IP是否合法 以前用PHP验证邮箱.URL.IP是否合法都是通过自己写正则来实现,但是有时候脑子发昏,可能会写出一个不是完全正确的正则,导致验证出错,今天发现原来PHP本身自带 ...
- filter_var() 验证邮箱、ip、url的格式 php
验证邮箱格式的正确与否:你的第一解决方案是什么呢? 不管你们怎么思考的:反正我首先想到的就是字符串查找看是否有@符号: 但是对于结尾的.com或者.net 亦或者.cn等等越来越多的域名验证感觉棘手: ...
- 常用验证正则:用户名、密码、邮箱、手机号、身份证(PHP和JavaScript)
日常开发中,常常会用到一些简单常用的正则表达式,用于判断一些常见的情况 下边,就列出五种(验证用户名,密码强度,邮箱格式,手机号格式和身份证格式)常见的情况 分成PHP版本和JavaScript两个版 ...
随机推荐
- 设置小窗口Activity
http://blog.csdn.net/xieyihua1994/article/details/52586270 我们知道Activity本身是没有界面的.所以activity类创建了一个窗口,开 ...
- python error: curl: (1) Protocol "'https" not supported or disabled in libcurl
python 调用curl访问一个网页时,出现error: curl: (1) Protocol "'https" not supported or disabled in lib ...
- 20145301《Java程序设计》第10周学习总结
20145301 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据 计算机网络 路由器和交换机组成了核心的计算机 ...
- arm-linux工具
arm-linux工具的功能如下: arm-linux-addr2line 把程序地址转换为文件名和行号.在命令行中给它一个地址和一个可执行文件名,它就会使用这个可执行文件的调试信息指出在给出的地址上 ...
- php7安装Memcached扩展
要安装 memcached,需要先安装依赖库 libmemcached wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/lib ...
- [CF730J]Bottles
题目大意:每个瓶子有一定的容积,以及一定的水量,问最少几个瓶子装满所有水,在此基础上还要最小化移动水的体积 第一问用贪心直接求第二问转化成背包问题设dp[i][j]表示前i桶水总容积为j的最多水量,这 ...
- brpc编译运行
自百度开源brpc后就一直在关注,看了下brpc都源码,感觉大牛写的果然不一样,由于之前我做c++游戏服务器开发,所以主要看了下网络底层代码,收益颇多,果然一天不学习就要落伍. 现在主要和大家说下我编 ...
- Spring mvc 字节流
public static void responseDownloadFile(HttpServletRequest request, HttpServletResponse response, Fi ...
- Matrix_QP(A_2SeqSum)
hdu_4686 题目大意:给出an,bn的递推,求ai*bi(i=0,1,--n-1)的和(an=a(n-1)*Ax+Ay, bn=b(n-1)*Bx+By, a0=A0, b0=B0, Ax,Bx ...
- 分布式文档存储数据库(MongoDB)副本集配置
副本集特征: N 个节点的集群 任何节点可作为主节点 所有写入操作都在主节点上 自动故障转移 自动恢复 相关文章: http://www.cnblogs.com/huangxincheng/archi ...