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. Android模拟器Intel Atom下载安装配置

    https://software.intel.com 在Android x86模拟器Intel Atom x86 System Image时提示Intel execute disable bit(xd ...

  2. jQuery中this 和 $(this)

    var node = $('#id'); node.click(function(){ this.css('display','block'); //报错  this是一个html元素,不是jquer ...

  3. LeetCode——remove-duplicates-from-sorted-list

    Question Given a sorted linked list, delete all duplicates such that each element appear only once. ...

  4. 连续取模-function

    2017-09-22 21:56:08 The shorter, the simpler. With this problem, you should be convinced of this tru ...

  5. 使用FireFox插件RESTClient工具POST方法?

    下面尝试用Firefox的restclient,来调取api 当然需要打开火狐浏览器安装restclient的插件https://addons.mozilla.org/en-US/firefox/ad ...

  6. docker 集群 笔记

    docker 集群 Docker 容器 移除所有的容器和镜像(大扫除) 用一行命令大扫除: docker kill $(docker ps -q) ; docker rm $(docker ps -a ...

  7. SpringMvc 笔记

    整理出来是 SpringMvc 笔记 方便以后查询 框架太多了 不经常使用 忘记的可能性很大 自己整理一套笔记 一看就明白了 1 对比 原始请求响应流程 1 发送请求 --> 2 控制层 --& ...

  8. linux一键安装nginx脚本

    #!/bin/sh echo "----------------------------------start install nginx ------------------------- ...

  9. Dive into Spring framework -- 了解基本原理(二)--设计模式-part1

    比较巧,自己在接触设计模式的时候,也刚开始学习spring,但可惜的是,真的仅仅在学习“用”spring,每天都沉浸在会痛快的完成spring各种配置的快乐之中,但对成长无用.其实当初就清楚,spri ...

  10. bzoj 1192 鬼谷子的钱袋 数学

    1192: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec  Memory Limit: 162 MB Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各 ...