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、身份证等)的更多相关文章

  1. C#验证邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP类等常用函数封装

    #region 验证邮箱验证邮箱 /**//// <summary> /// 验证邮箱 /// </summary> /// <param name="sour ...

  2. php使用过滤器filter_var轻松验证邮箱url和ip地址等

    以前使用php的时候还不知道有过滤器filter这玩意,那时候判断邮箱.url和ip地址格式是否符合都是用正则表达式.后来随着使用的逐渐深入,才知道在php中也可以使用内置的函数库过滤器filter来 ...

  3. web开发常用的js验证,利用正则表达式验证邮箱、手机、身份证等输入

    正则表达式验证 //邮箱 \-])+\.)+([a-zA-Z0-]{,})+$/; email = document.getElementById("email").value; ...

  4. C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP (转)

    namespace YongFa365.Validator { using System; using System.Text.RegularExpressions; /**//// <summ ...

  5. 验证中文、英文、电话、手机、邮箱、数字、数字和字母、Url地址和Ip地址的正则表达式

    Helper类代码 public class Helper { #region 单列循环 private Helper() { } private static Helper instance = n ...

  6. php自带验证邮箱 url ip函数

    以前用PHP验证邮箱.URL.IP是否合法都是通过自己写正则来实现,但是有时候脑子发昏,可能会写出一个不是完全正确的正则,导致验证出错,今天发现原来PHP本身自带了验证邮箱.URL.IP是否合法的函数 ...

  7. PHP自带方法验证邮箱、URL、IP是否合法

    PHP验证邮箱.URL.IP是否合法 以前用PHP验证邮箱.URL.IP是否合法都是通过自己写正则来实现,但是有时候脑子发昏,可能会写出一个不是完全正确的正则,导致验证出错,今天发现原来PHP本身自带 ...

  8. filter_var() 验证邮箱、ip、url的格式 php

    验证邮箱格式的正确与否:你的第一解决方案是什么呢? 不管你们怎么思考的:反正我首先想到的就是字符串查找看是否有@符号: 但是对于结尾的.com或者.net 亦或者.cn等等越来越多的域名验证感觉棘手: ...

  9. 常用验证正则:用户名、密码、邮箱、手机号、身份证(PHP和JavaScript)

    日常开发中,常常会用到一些简单常用的正则表达式,用于判断一些常见的情况 下边,就列出五种(验证用户名,密码强度,邮箱格式,手机号格式和身份证格式)常见的情况 分成PHP版本和JavaScript两个版 ...

随机推荐

  1. 【c++ primer, 5e】函数重载

    [函数重载] Java中的重载一般是指重载构造器,或是子类覆写父类的方法:C++中的重载稍微复杂一些. 定义重载函数 典型的数据库应用. Record lookup(const Account& ...

  2. Python3.x:logging模块对运行过程记录

    Python3.x:logging模块对运行过程记录 示例: import logging # 设置 logger = logging.getLogger() #set loghandler #默认路 ...

  3. 20135320赵瀚青LINUX第三章读书笔记

    第三章 进程管理 3.1 进程 进程的定义: 是处于执行期的程序以及它所包含的资源的总称. 线程的定义: 是在进程中活动的对象. 每个线程都拥有一个独立的程序计数器.进程栈和一组进程寄存器. 内核调度 ...

  4. HandyJSON第三方库的日常使用与错误记录

    一.错误提示 1.更新Xcode10.2,Swift5.0出现错误提示 Undefined symbols for architecture x86_64: "_swift_getField ...

  5. React绑定this的三种方式

    React可以使用React.createClass.ES6 classes.纯函数3种方式构建组件.使用React.createClass会自动绑定每个方法的this到当前组件,但使用ES6 cla ...

  6. C#开发自己的Web服务器

    介绍 我们将学习如何写一个简单的web服务器,用于响应知名的HTTP请求(GET和POST),用C#发送响应.然后,我们从网络访问这台服务器,这次我们会说“Hello world!” 背景 HTTP协 ...

  7. An Example for Javascript Function Scoping and Closure

    1. An Real World Example In the patron detail page of the CRM system I'm working with, there’re larg ...

  8. 设置CentOS7虚拟机使用静态IP上网

    一.在VMware里,依次点击”编辑“ - ”虚拟网络编辑器“,如下图,我选择的是NAT模式: 为了能够使用静态IP,这里不要勾选”使用本地DHCP服务将IP分配给虚拟机“这个选项.然后是配置子网ip ...

  9. Elasticsearch之中文分词器

    前提 什么是倒排索引? Elasticsearch之分词器的作用 Elasticsearch之分词器的工作流程 Elasticsearch之停用词 Elasticsearch的中文分词器 1.单字分词 ...

  10. 解题报告:poj1321 棋盘问题 - 搜索

    棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 51262 Accepted: 24785 Description 在一 ...