using System;
using System.Text.RegularExpressions; namespace Util
{
public static class @string
{
#region 验证字符串是否符合正则表达式MachRegex
/// <summary>
/// 验证字符串是否符合正则表达式MachRegex
/// </summary>
/// <param name="regex">正则表达式</param>
/// <param name="str">字符串</param>
/// <returns>是否符合 true 或者 false</returns>
private static bool MachRegex(string regex, string str)
{
Regex reg = new Regex(regex);
return reg.IsMatch(str);
}
#endregion #region 验证URL
/// <summary>
/// 验证URL
/// </summary>
/// <param name="str">要验证的字符串</param>
/// <returns>是否符合 true 或者 false</returns>
public static bool IsUrl(this string str)
{
return MachRegex(@"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$", str);
}
#endregion #region 验证IP地址
/// <summary>
/// 验证IP地址
/// </summary>
/// <param name="str">要验证的字符串</param>
/// <returns>是否符合 true 或者 false</returns>
public static bool IsIpAddress(this string str)
{
return MachRegex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$", str);
}
#endregion #region 验证邮编
/// <summary>
/// 验证邮编
/// </summary>
/// <param name="str">要验证的字符串</param>
/// <returns>是否符合 true 或者 false</returns>
public static bool IsZipCode(this string str)
{
return MachRegex(@"^[0-9]{6}$", str);
}
#endregion #region 验证是否汉字
/// <summary>
/// 验证是否汉字
/// </summary>
/// <param name="str">要验证的字符串</param>
/// <returns>是否符合 true 或者 false</returns>
public static bool IsChineseChar(this string str)
{
return MachRegex(@"^[\u4e00-\u9fa5]{0,}$", str);
}
#endregion #region 验证是否Email地址
/// <summary>
/// 验证是否Email地址
/// </summary>
/// <param name="str">要验证的字符串</param>
/// <returns>是否符合 true 或者 false</returns>
public static bool IsEmail(this string str)
{
return MachRegex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", str);
}
#endregion #region 验证是否电话号码
/// <summary>
/// 验证是否电话号码
/// </summary>
/// <param name="str">要验证的字符串</param>
/// <returns>是否符合 true 或者 false</returns>
public static bool IsTelNumber(this string str)
{
return MachRegex(@"^((\d{3,4}-)|\d{3.4}-)?\d{7,8}$", str);
}
#endregion #region 是否是手机号码
/// <summary>
/// 是否是手机号码
/// </summary>
/// <param name="val"></param>
public static bool IsMobile(this string val)
{
return Regex.IsMatch(val, @"^1[358]\d{9}$", RegexOptions.IgnoreCase);
}
#endregion #region 验证用户密码。正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。
/// <summary>
/// 验证用户密码。正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。
/// </summary>
/// <param name="str">要验证的字符串</param>
/// <returns>是否符合 true 或者 false</returns>
public static bool IsStandardPassword(this string str)
{
return MachRegex(@"^[a-zA-Z]\w{5,17}$", str);
}
#endregion #region 身份证有效性验证
/// <summary>
/// 身份证验证
/// </summary>
/// <param name="Id">身份证号</param>
/// <returns></returns>
public static bool IsIDCard(this string Id)
{
if (Id.Length == )
{
bool check = CheckIDCard18(Id);
return check;
}
else if (Id.Length == )
{
bool check = CheckIDCard15(Id);
return check;
}
else
{
return false;
}
} /// <summary>
/// 18位身份证验证
/// </summary>
/// <param name="Id">身份证号</param>
/// <returns></returns>
private static bool CheckIDCard18(string Id)
{
long n = ;
if (long.TryParse(Id.Remove(), out n) == false || n < Math.Pow(, ) || long.TryParse(Id.Replace('x', '').Replace('X', ''), out n) == false)
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove()) == -)
{
return false;//省份验证
} string birth = Id.Substring(, ).Insert(, "-").Insert(, "-");
System.DateTime time = new System.DateTime();
if (System.DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
} string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(','); string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(','); char[] Ai = Id.Remove().ToCharArray(); int sum = ;
for (int i = ; i < ; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
} int y = -; Math.DivRem(sum, , out y); if (arrVarifyCode[y] != Id.Substring(, ).ToLower())
{
return false;//校验码验证
}
return true;//符合GB11643-1999标准
} /// <summary>
/// 15位身份证验证
/// </summary>
/// <param name="Id">身份证号</param>
/// <returns></returns>
private static bool CheckIDCard15(string Id)
{
long n = ;
if (long.TryParse(Id, out n) == false || n < Math.Pow(, ))
{
return false;//数字验证
} string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove()) == -)
{
return false;//省份验证
} string birth = Id.Substring(, ).Insert(, "-").Insert(, "-");
System.DateTime time = new System.DateTime();
if (System.DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
return true;//符合15位身份证标准
}
#endregion #region 验证是否合法域名
/// <summary>
/// 验证是否合法域名
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool IsHost(this string url)
{
string pattern = @"^[0-9a-zA-Z-]*\.(com\.tw|com\.cn|com\.hk|net\.cn|org\.cn|gov\.cn|ac\.cn|bj\.cn|sh\.cn|tj\.cn|cq\.cn|he\.cn|sx\.cn|nm\.cn|ln\.cn|jl\.cn|hl\.cn|js\.cn|zj\.cn|ah\.cn|fj\.cn|jx\.cn|sd\.cn|ha\.cn|hb\.cn|hn\.cn|gd\.cn|gx\.cn|hi\.cn|sc\.cn|gz\.cn|yn\.cn|xz\.cn|sn\.cn|gs\.cn|qh\.cn|nx\.cn|xj\.cn|tw\.cn|hk\.cn|mo\.cn|com|net|org|biz|info|cn|mobi|name|sh|ac|io|tw|hk|ws|travel|us|tm|cc|tv|la|in|asia|me|net\.ru)$";
return Regex.IsMatch(url, pattern, RegexOptions.IgnoreCase);
}
#endregion
}
}

C#中常用的字符串验证的更多相关文章

  1. java中常用的字符串的截取方法

    java中常用的字符串的截取方法   1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int l ...

  2. LoadRunner中常用的字符串操作函数

    LoadRunner中常用的字符串操作函数有:                strcpy(destination_string, source_string);               strc ...

  3. Js中常用的字符串,数组,函数扩展

    由于最近辞职在家,自己的时间相对多一点.所以就根据prototytpeJS的API,结合自己正在看的司徒大神的<javascript框架设计>,整理了下Js中常用一些字符串,数组,函数扩展 ...

  4. php中常用的字符串查找函数strstr()、strpos()实例解释

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...

  5. mysql中常用的字符串函数

    写在分割线之前,个人以为,数据库应该具备简单的的数据加工能力.如同食品在吃之前,是要经过很多到工序的,有经过初加工.粗加工.精加工.深加工等.那么mysql也应该并必须担任起数据初加工以及粗加工的责任 ...

  6. PHP中常用的字符串格式化函数总结

    注意:在PHP中提供的字符串函数处理的字符串,大部分都不是在原字符串上修改,而是返回一个格式化后的新字符串. 一.取出空格和字符串填补函数 空格也是一个有效的字符,在字符串中也会占据一个位置.用户在表 ...

  7. C语言中常用的字符串操作函数

    程序开头要声明 #include <string.h> 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char ...

  8. PHP开发中常用的字符串操作函数

    1,拼接字符串 拼接字符串是最常用到的字符串操作之一,在PHP中支持三种方式对字符串进行拼接操作,分别是圆点.分隔符{}操作,还有圆点等号.=来进行操作,圆点等号可以把一个比较长的字符串分解为几行进行 ...

  9. netty系列之:netty中常用的字符串编码解码器

    目录 简介 netty中的字符串编码解码器 不同平台的换行符 字符串编码的实现 总结 简介 字符串是我们程序中最常用到的消息格式,也是最简单的消息格式,但是正因为字符串string太过简单,不能附加更 ...

随机推荐

  1. C#自定义控件 ————进度条

    先看看样式 一个扇形的进度条 对外公开的方法和属性 事件 value_change;//值改变时触发的事件progress_finshed;//进度条跑完时触发的事件 属性 Max_value//获取 ...

  2. React中state和props的区别

    props和state都是用于描述component状态的,并且这个状态应该是与显示相关的. State 如果component的某些状态需要被改变,并且会影响到component的render,那么 ...

  3. SpringBoot04 项目热部署详解

    1 热部署 优点:适用于本地和线上.无需重启服务器 2 热部署和热加载的联系和区别 2.1 联系 不用重启服务器就可以编译和部署项目 都是基于Java的类加载器实现 2.2 字面区别 热部署在服务器运 ...

  4. 17、GATK使用简介 Part2/2

    转载:http://blog.sina.com.cn/s/blog_6721167201018jik.html Change Logs: 13/01/12: 增加了一篇文献,外加一些无聊的修改.12/ ...

  5. 10、RNA-seq for DE analysis training(Mapping to assign reads to genes)

    1.Goal of mapping 1)We want to assign reads to genes they were derived from 2)The result of the mapp ...

  6. 【maven setting.xml】

    <!--声明语句--> <?xml version="1.0" encoding="UTF-8"?> <settings xmln ...

  7. C#识别图中二维码

    1.在NuGet中添加 ZXing.Net 2.实例代码 /// <summary> /// 识别图中二维码 /// </summary> /// <param name ...

  8. SNAT端口转发配置

    需求说明 在只有外网地址的机器上也能正常访问内网地址 配置过程 环境网络信息 网络名称 网络地址 外网 192.168.200.0/24 (网关:192.168.200.251) 内网 92.0.0. ...

  9. Educational Codeforces Round 57D(DP,思维)

    #include<bits/stdc++.h>using namespace std;char s[100007];long long a[100007];long long dp[100 ...

  10. PAT L3-010【完全二叉树】

    静态建树判一下1-n是不是为空就好了,如果有空的  就说明不是complete binary tree (和线段树建树差不多啊)Left=2*root:Right=2*root+1 #include ...