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两个版 ...
随机推荐
- imx6q android 添加开机启动脚本
1.在xx/out/target/product/sabresd_6dq/root/init.rc中添加以下内容 ========================================== ...
- RocEDU.阅读.写作《乌合之众》(一)
序言 作者在序言里主要论述了时代演变的内在原因,表明对群体进行研究的重要性,阐述了研究群体行为特征时的研究方法,并概述了群体的发展过程. 造成文明变革的唯一重要变化,是影响到思想.观念和信仰的变化.目 ...
- 20145333《Java程序设计》第3次实验报告
20145333<Java程序设计>第3次实验报告 实验内容 使用 git 上传代码 使用 git 相互更改代码 实现代码的重载 实验步骤 git设置用户名邮箱,ssh公钥 用git上传代 ...
- SaltStack高可用multi-master-第十三篇
multi-master官方介绍 As of Salt 0.16.0, the ability to connect minions to multiple masters has been made ...
- 数据结构实习 Problem H 迷宫的最短路径
数据结构实习 Problem H 迷宫的最短路径 题目描述 设计一个算法找一条从迷宫入口到出口的最短路径. 输入 迷宫的行和列m n 迷宫的布局 输出 最短路径 样例输入 6 8 0 1 1 1 0 ...
- caohaha's stuff
2017-08-20 11:12:29 writer:pprpCCPC预选赛水平太菜了,去不了了 这个是一个找规律的题目,题意一开始也很难理解 题意描述: 给你一个数,比如说1,在一个坐标系中你需要用 ...
- 如何查看一个进程打开哪些fd及对应的文件或套接字操作
- 从git获取项目代码
1.先复制项目的SSH链接备用 2.在你要放置项目的地方git bash here 3.按照以下步骤走: $ git clone YourSSHAddress // clone项目 $ ls // 查 ...
- 英语每日阅读---5、VOA慢速英语(翻译+字幕+讲解):美国人口普查局表示美国人受教育程度提升
英语每日阅读---5.VOA慢速英语(翻译+字幕+讲解):美国人口普查局表示美国人受教育程度提升 一.总结 一句话总结: a.Thirty-four percent - college degree: ...
- Tomcat服务部署步骤
Tomcat服务部署步骤 1. 2. 3. tar -zxvf apache-tomcat-7.0.68.tar.gz,然后修改文件夹名称为需要的名称, 使用mv命令 4. 删除 /webapps/R ...