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两个版 ...
随机推荐
- Yahoo网站性能优化的34条规则
摘自:http://blog.chinaunix.net/uid/20714478/cid-74195-list-1.html Yahoo网站性能优化的34条规则 1.尽量减少HTTP请求次数 终端用 ...
- 20145314郑凯杰 《Java程序设计》实验四 实验报告
20145314郑凯杰 <Java程序设计>实验四 实验报告 实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用 ...
- Android执行shell命令 top ps
Android执行shell命令 一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/ ...
- Could not reserve enough space for 1572864KB object heap
This problem might be caused by incorrect configuration of the daemon.For example, an unrecognized j ...
- Labview 查看一次while循环运行的时间
在while循环中增加一个移位寄存器,移位寄存器的初始值使用时间计数器,在while循环里面增加一个减法Vi,再增加一个时间计数器,两者做差,最后显示差值. 在这里只能显示大概运行时间.如下图.
- NSURLSession 的学习资料
一个nsurlsession的一些学习资料 http://www.cocoachina.com/ios/20161018/17785.html
- caffe平台快速搭建:caffe+window7+vs2013
caffe平台快速搭建:caffe+window7+vs2013 1.caffe-master下载 采用微软提供Windows工具包(caffe-master),下载地址:https://github ...
- [Linux]pycharm在Linux环境下安装
之前转载了一个在Windows环境下pycharm专业破解的安装的文章,今天为了在Linux环境下安装使用odoo10,所以尝试在Linux环境下安装pycharm专业破解版看看. windows下安 ...
- 锁(1)-- java锁
前言: 锁分3种:java锁.分布式锁.DB锁 在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 公平锁/非公平锁 可重入锁 独享锁/共享锁 互斥锁 ...
- cocos2d-x入门三 分层设计框架
helloworld就是一个完整的框架,大致分为四个层次如下: 导演-------场景------图层-----精灵 Director-----Scene----Layer----Sprite 导演类 ...