using System;
using System.Text;
using System.Text.RegularExpressions; namespace Dachie.Common
{
/// <summary>
/// Common 的摘要说明。
/// </summary>
public class RegexComm
{
//private const string REG_DATE = @"^(\d{2}|\d{4})[\-\/]((0?[1-9])|(1[0-2]))[\-\/]((0?[1-9])|((1|2)[0-9])|30|31)$";
private const string REG_DATE = @"^(\d{2}|\d{4})((0[1-9])|(1[0-2]))((0[1-9])|((1|2)[0-9])|30|31)$";
private const string REG_PHONE = @"^((0[0-9]{2,3}){0,1}([0-9]{7,8}))$";
private const string REG_EMAIL = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
private const string REG_MOBILE = @"(^0{0,1}13[0-9]{9}$)";
private const string REG_IDCARD = @"^([0-9]{14}|[0-9]{17})(x|[0-9]){1}$";
private const string REG_TIME = @"^((([0-1]?[0-9])|(2[0-3]))([\:])([0-5][0-9]))$"; #region 半角验证
/// <summary>
/// 半角验证
/// </summary>
/// <param name="str">验证的字符串</param>
/// <returns></returns>
public static bool IsDBC(string str)
{
UTF8Encoding encoding = new UTF8Encoding();
int byteCount = encoding.GetByteCount(str);
int strLen = str.Length; if(strLen == byteCount)
{
return true;
} return false;
}
#endregion #region 全角验证
/// <summary>
/// 全角验证
/// </summary>
/// <param name="str">验证的字符串</param>
/// <returns></returns>
public static bool IsSBC(string str)
{
UTF8Encoding encoding = new UTF8Encoding();
int byteCount = encoding.GetByteCount(str);
int strLen = str.Length; if(byteCount == strLen * 3 )
{
return true;
} return false;
}
#endregion #region 日期字符串有效性验证
/// <summary>
/// 日期字符串有效性验证
/// </summary>
/// <param name="date">日期字符串</param>
/// <returns>有效:true,否则:false</returns>
public static bool IsValidDate(string date)
{
return Regex.IsMatch(date, RegexComm.REG_DATE);
}
#endregion #region Email有效性验证
/// <summary>
/// Email有效性验证
/// </summary>
/// <param name="email">Email字符串</param>
/// <returns>有效:true,否则:false</returns>
public static bool IsValidEmail(string email)
{
return Regex.IsMatch(email, RegexComm.REG_EMAIL);
}
#endregion #region 电话号码有效性验证
/// <summary>
/// 电话号码有效性验证
/// </summary>
/// <param name="phone">电话号码字符串</param>
/// <returns>有效:true,否则:false</returns>
public static bool IsVaildPhone(string phone)
{
return Regex.IsMatch(phone, RegexComm.REG_PHONE);
}
#endregion #region 手机号码有效性验证
/// <summary>
/// 手机号码有效性验证
/// </summary>
/// <param name="mobile">手机号码字符串</param>
/// <returns>有效:true,否则:false</returns>
public static bool IsValidMobile(string mobile)
{
return Regex.IsMatch(mobile,REG_MOBILE);
}
#endregion #region 身份证号有效性验证
/// <summary>
/// 身份证号有效性验证
/// </summary>
/// <param name="idCard">身份证号字符串</param>
/// <returns>有效:true,否则:false</returns>
public static bool IsValidIdCard(string idCard)
{
return Regex.IsMatch(idCard, RegexComm.REG_IDCARD);
}
#endregion #region 日期字符串转换成日期对象
/// <summary>
/// 日期字符串转换成日期对象
/// </summary>
/// <param name="date">日期字符串</param>
/// <returns>日期对象</returns>
public static DateTime CastDateTime(string date)
{
StringBuilder builder = new StringBuilder();
builder.Append(date.Substring(0,4));
builder.Append("-");
builder.Append(date.Substring(4,2));
builder.Append("-");
builder.Append(date.Substring(6,2)); return Convert.ToDateTime(builder.ToString());
}
#endregion #region 日期对象转化成日期字符串
/// <summary>
/// 日期对象转化成日期字符串
/// </summary>
/// <param name="date">日期对象</param>
/// <returns>日期字符串</returns>
public static string CastDateTime(DateTime date)
{
string strDate = date.ToString("yyyy-MM-dd");
strDate = strDate.Replace("-","");
return strDate;
}
#endregion #region 时间格式验证
/// <summary>
/// 时间格式验证
/// </summary>
/// <param name="time">时间字符串</param>
/// <returns>正确:true,错误:false</returns>
public static bool IsValidTime(string time)
{
return Regex.IsMatch(time,REG_TIME);
}
#endregion
}
}

C#检验数据有效性验证类的更多相关文章

  1. 使用FluentValidation来进行数据有效性验证

    之前我介绍过了使用系统自带的Data Annotations来进行数据有效性验证,今天在CodePlex上逛的时候,发现了一个非常简洁好用的库:FluentValidation 由于非常简洁,就直接拿 ...

  2. 个人永久性免费-Excel催化剂功能第60波-数据有效性验证增强版,补足Excel天生不足

    Excel在数据处理.数据分析上已经是公认的最好用的软件之一,其易用性和强大性也吸引无数的初中高级用户每天都在使用Excel.但这些优点的同时,也带出了一些问题,正因为其不同于一般的专业软件,需要专业 ...

  3. Excel 如何引用某表格中的某一列作为数据有效性验证

    1. 首先把数据有效性的列表加入到某个表格中.如下图所示:此表格名称为表5 2. 然后定义名称:公式--定义名称 如下填入信息: 3. 然后再数据有效性验证中输入如下信息即可:

  4. JavaScript 数据验证类

    JavaScript 数据验证类 /* JavaScript:验证类 author:杨波 date:20160323 1.用户名验证 2.密码验证 3.重复密码验证 4.邮箱验证 5.手机号验证 6. ...

  5. C# 通用验证类 支持 WPF,MVC,Winform

    验证方式,   通过继承 IDataErrorInfo接口 和 DataAnnotations 解释标记语言而实现, 为了能在WPF上通用,所了也要继承属性更改通知接口INotifyPropertyC ...

  6. C# - DataValid数据验证类

    从EasyCode 摘取下来的数据验证类 using System; using System.Collections.Generic; using System.Text; namespace Le ...

  7. php表单数据验证类

    非常好用方便的表单数据验证类 <?php //验证类 class Fun{ function isEmpty($val) { if (!is_string($val)) return false ...

  8. C# System.Attribute(验证类)

    本文以一个项目中通用的验证类来举例说明如何使用自定义Attribute来扩展元数据.  在项目中,我们为了保证各个层次之间的松藕合,通常把在各个层次之间传递数据的封装在一个称为实体类的类中,比如Act ...

  9. JS表单验证类HTML代码实例

    以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮 ...

随机推荐

  1. 数独的C++解法

    grid.h #ifndef _GRID_H_ #define _GRID_H_ #include <set> #include <cstddef> class Grid { ...

  2. c++中动态尾随内存的技巧和定位new

    c 和 c++ 最大的特点就是对内存的自由操作,数据类型,其实都是对内存的一种解释方式.C语言中常用的一个技巧就是尾随数据,网络编程中经常会用到这个特性, 特别是以前写完成端口的时候,这个特性肯定是会 ...

  3. Beyond Compare 使用介绍

    Beyond Compare 背景 平时工作中对于源代码都是使用SVN来管理,在线状态下工作的很好,但是有时候离线状态下,对于多个版本之间的代码合并就比较麻烦.尤其是涉及到多人协作时更是如此. 所以找 ...

  4. [DevExpress]SplitContainerControl使用小计

    1.修改成纵向分割 Horizontal = false; 2.设置伸缩箭头 3.固定某个PANEL大小 最大化后依然保持着比例 4.隐藏某个PANEL splitContainerControl1. ...

  5. [Linux]查看本机IP

    命令: ~$ ip addr showor~$ ipconfig 

  6. OpenDialog获取文件名

    //OpenDialog获取文件 procedure TForm2.Button1Click(Sender: TObject); begin File_Path:=''; if OpenDialog1 ...

  7. VB6-系统打印常识

    在一次做图片打印的时候,对位置的调整老是不得法,后来通过CBM666老师的帮助才解决问题,分享以下他给的帮助.     , , picA.Width , picA.Height Printer.End ...

  8. Django基本介绍

    Django板块分类: 1.urls.py  网址的入口(关联到views.py中的一个函数) 2.views.py 处理用户发起的请求,从urls.py中对应过来,通过渲染templates中的网页 ...

  9. Web API 返回json文件的2中不用方式

    //方法一:直接返回序列化后的json文件 public static HttpResponseMessage ConvertToJson(this Object obj) { String str= ...

  10. hdu 5172 GTY's gay friends

    GTY's gay friends 题意:给n个数和m次查询:(1<n,m<1000,000);之后输入n个数值(1 <= ai <= n):问下面m次查询[L,R]中是否存在 ...