C#检验数据有效性验证类
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#检验数据有效性验证类的更多相关文章
- 使用FluentValidation来进行数据有效性验证
之前我介绍过了使用系统自带的Data Annotations来进行数据有效性验证,今天在CodePlex上逛的时候,发现了一个非常简洁好用的库:FluentValidation 由于非常简洁,就直接拿 ...
- 个人永久性免费-Excel催化剂功能第60波-数据有效性验证增强版,补足Excel天生不足
Excel在数据处理.数据分析上已经是公认的最好用的软件之一,其易用性和强大性也吸引无数的初中高级用户每天都在使用Excel.但这些优点的同时,也带出了一些问题,正因为其不同于一般的专业软件,需要专业 ...
- Excel 如何引用某表格中的某一列作为数据有效性验证
1. 首先把数据有效性的列表加入到某个表格中.如下图所示:此表格名称为表5 2. 然后定义名称:公式--定义名称 如下填入信息: 3. 然后再数据有效性验证中输入如下信息即可:
- JavaScript 数据验证类
JavaScript 数据验证类 /* JavaScript:验证类 author:杨波 date:20160323 1.用户名验证 2.密码验证 3.重复密码验证 4.邮箱验证 5.手机号验证 6. ...
- C# 通用验证类 支持 WPF,MVC,Winform
验证方式, 通过继承 IDataErrorInfo接口 和 DataAnnotations 解释标记语言而实现, 为了能在WPF上通用,所了也要继承属性更改通知接口INotifyPropertyC ...
- C# - DataValid数据验证类
从EasyCode 摘取下来的数据验证类 using System; using System.Collections.Generic; using System.Text; namespace Le ...
- php表单数据验证类
非常好用方便的表单数据验证类 <?php //验证类 class Fun{ function isEmpty($val) { if (!is_string($val)) return false ...
- C# System.Attribute(验证类)
本文以一个项目中通用的验证类来举例说明如何使用自定义Attribute来扩展元数据. 在项目中,我们为了保证各个层次之间的松藕合,通常把在各个层次之间传递数据的封装在一个称为实体类的类中,比如Act ...
- JS表单验证类HTML代码实例
以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮 ...
随机推荐
- Headfirst设计模式的C++实现——迭代器(Iterator)
iterator.h #ifndef _ITERATOR_H_ #define _ITERATOR_H_ #include "menu_item.h" class Iterator ...
- 《APUE》第四章笔记(3)
文件系统 首先我们应该知道一个磁盘可以划分为多个分区,而每个分区就可以包含一个文件系统.UNIX的文件系统是这样的: 而我们主要关心的是i节点和数据块.i节点是固定长度的记录项,它包含有关文件的大部分 ...
- Windows 8上强制Visual Studio以管理员身份运行
原文链接:http://edi.wang/post/2013/2/28/force-visual-studio-always-run-as-admin-on-windows-8 Windows 8的一 ...
- Asp.net自带导出方法
///datatable数据源 filename绝对路径 如:E:\\***.xls DataTable.WriteXml(fileName)
- CSS弹性盒模型 box-flex
目前没有浏览器支持boc-flex属性. Firefox支持代替的-moz-box-flex属性 Safari.Opera以及Chrome支持替代的-webkit-box-flex属性 box-fle ...
- uvision4 ide已停止工作
情景描述: 笔者安装了新系统WIN8.1,装上了MDKV4.72.MDK编译程序可以正常工作,可是只要当我“下载程序”或者“调试程序”的时候就提示“uvision4 ide已停止工作”,迫不得已只能关 ...
- Net中的AOP
.Net中的AOP系列之<单元测试切面> 返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle ...
- 我的PHP之旅--PHP的判断、循环语句
if语句 <?php if ($a = "some string") { // 就算括号中不是bool值,php也会自动转换为bool值 上一节写过各个类型转换bool值 / ...
- Hadoop集群(第5期)_Hadoop安装配置
1.1 Hadoop简介 Hadoop是Apache软件基金会旗下的一个开源分布式计算平台.以Hadoop分布式文件系统(HDFS,Hadoop Distributed Filesystem)和Map ...
- SQl为表添加和删除列
1.删除列: Alter Table TransBetRecord drop column ToProjectCode 2.添加列: Alter Table TransBetRecord ...