C#验证字符串是否是数字,是否包括中文,是否是邮箱格式,是否是电话格式
using System;
using System.Web;
using System.Text;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
public class ValidateHelper
{
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?
[0-9]+$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$");
private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
private static Regex RegTell = new Regex("^(([0-9]{3,4}-)|[0-9]{3.4}-)?
[0-9]{7,8}$"); :25[0-5]|2[0-4]\\d|[01]?\\d? \\d))$">\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?|[a-zA-z]+://((?:(?:25[0-5]|2[0-4]\\d|[01]? \\d? \\d)\\.){3}(? :25[0-5]|2[0-4]\\d|[01]?\\d?\\d))$
private static Regex RegSend = new Regex("[0-9]{1}([0-9]+){5}");
private static Regex RegUrl = new Regex("^[a-zA-z]+://(
private static Regex RegMobilePhone = new Regex("^13|15|18[0-9]{9}$");
private static Regex RegMoney = new Regex("^[0-9]+|[0-9]+[.]?[0-9]+$");
#region 数字字符串检查
/// <summary>
/// 是否数字字符串
/// </summary>
/// <param name="inputData">输入字符串</param>
public static bool IsNumber(string inputData)
{
if (!string.IsNullOrEmpty(inputData))
{
Match m = RegNumber.Match(inputData);
return m.Success;
}
else
{
return false;
}
}
/// <summary>
/// 是否数字字符串 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
public static bool IsDecimal(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
}
/// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecimalSign.Match(inputData);
return m.Success;
}
#endregion
#region 中文检測
/// <summary>
/// 检測是否有中文字符
/// </summary>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
}
#endregion
#region 邮件地址
/// <summary>
/// 是否是邮箱
/// </summary>
/// <param name="inputData">输入字符串</param>
public static bool IsEmail(string inputData)
{
Match m = RegEmail.Match(inputData);
return m.Success;
}
#endregion
#region 电话,邮政编码,网络地址,手机号码,价格
/// <summary>
/// 验证是否是电话
/// </summary>
public static bool IsPhone(string inputDate)
{
if (!string.IsNullOrEmpty(inputDate))
{
Match m = RegTell.Match(inputDate);
return m.Success;
}
else
{
return false;
}
}
/// <summary>
/// 是否是邮政编码
/// </summary>
public static bool IsSend(string inputDate)
{
if (!string.IsNullOrEmpty(inputDate))
{
Match m = RegSend.Match(inputDate);
return m.Success;
}
else
{
return false;
}
}
/// <summary>
/// 是否是网络地址
/// </summary>
public static bool IsUrl(string inputDate)
{
Match m = RegUrl.Match(inputDate);
return m.Success;
}
/// <summary>
/// 是否是手机号码
/// </summary>
public static bool IsMobilePhone(string inputDate)
{
Match m = RegMobilePhone.Match(inputDate);
return m.Success;
}
/// <summary>
/// 是否是价格
/// </summary>
public static bool IsMoney(string inputDate)
{
Match m = RegMoney.Match(inputDate);
return m.Success;
}
#endregion
#region 是否是时间格式
/// <summary>
/// 推断一个字符串是否时间格式
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDateTime(string inputData)
{
try
{
Convert.ToDateTime(inputData);
return true;
}
catch
{
return false;
}
}
#endregion
}
C#验证字符串是否是数字,是否包括中文,是否是邮箱格式,是否是电话格式的更多相关文章
- php验证字符串是否以逗号隔开包括中文字符串
if(preg_match('/^[\x{4e00}-\x{9fa5}\w]+(,[\x{4e00}-\x{9fa5}\w]+)*$/u','体育,娱乐')){ echo 'ok';}
- JavaScript验证字符串只能包含数字或者英文字符的代码实例
验证字符串只能包含数字或者英文字符的代码实例:本章节分享一段代码实例,它实现了验证字符串内容是否只包含英文字符或者数字.代码实例如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- php 正则验证字符串是否为数字
PHP 正则验证字符串是否为数字 方法一: php中利用正则表达式验证字符串是否为数字一件非常容易的事情,最主要的是如何写好正则表达式以及掌握正则表达式的写法,在此利用正则表达式的方式来列举一下判断数 ...
- java正则表达式应用--验证字符串是否为数字(转载)
首先说一下java正则表达式的重点概念: 第一.相关类:Pattern.Matcher 第二.典型的调用顺序是 Pattern p = Pattern.compile("a*b") ...
- java判断字符串是否为数字,包括负数
/** * 判断是否为数字,包含负数情况 * @param str * @return */ private boolean isNumeric(String str){ Boolean flag = ...
- c#拆分字符串英文和数字(包括国外所以文字)
先创建一个类: /// <summary> /// 字符串分析 /// </summary> interface IStringAna { /// <summary> ...
- asp.net检查验证字符串是否为纯数字方法小结
原文 asp.net检查验证字符串是否为纯数字方法小结 在asp.net中验证字符串是不是为数字我们没有像php中那么多丰富的函数来直接使用,这里我整理了一些比较实例的验证字符串是否为纯数字方法代码 ...
- C#判断字符串是否为数字字符串
在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现.[方法一]:使用 try{} catch{} 语句. 我们可以在try语句块中试图将str ...
- 验证一个字符串是否由数字组成(Java)
public class StringDemo{ public static void main(String args[]){ String str ="12343264sd6223&qu ...
随机推荐
- C#中调用Windows API时的数据类型对应关系
原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE=System.UInt16 CHAR=System. ...
- Linux下搭建Hadoop集群
本文地址: 1.前言 本文描述的是如何使用3台Hadoop节点搭建一个集群.本文中,使用的是三个Ubuntu虚拟机,并没有使用三台物理机.在使用物理机搭建Hadoop集群的时候,也可以参考本文.首先这 ...
- Android应用开发经常使用知识
在其它站点看到的,Mark一下 1.近期打开的应用不在近期任务列表中显示 android:excludeFromRecents="true" 设置为true,则排除在近期任务列表之 ...
- WPF(布局)
WPF编程学习——布局 本文目录 1.布局简介 2.面板(Panel) 3.视图框(Viewbox) 4.滚动视图控件(ScrollViewer) 5.公共布局属性 1.布局简介 应用程序界面 ...
- 集合简单总结 ArrayList、List、Hashtable、Dictionary
============================ 集合综述 ============================== 1.什么是泛型: 泛型就是限制了操作类型,意思如下: ...
- linux: /usr/bin/ld: cannot find -lloc
/usr/bin/ld: cannot find -lloc ld链接库的时候没发现loc这个库-lloc本事不是文件名字,要去找这个库就搜索libloc, loc, 不能搜索lloc. /usr1/ ...
- 认识axure组件区域
组件区域也叫做部件区域,英文为widgets,还有人称之为控件区域,组件是axure事先准备好的网站项目常用的零件,比如一些基本的页面元素 Axure默认存在2个组件库,分别为线框图和流程图.同时我们 ...
- 基于visual Studio2013解决C语言竞赛题之1056素数序列
题目 解决代码及点评 /* 56. 编程序求3至39之间满足下列条件的各组素数:每组有3个素数,第2个比第一个大2,第3个比第2个大4.例如 5,7,11就是满足条件的一组. 要求: ...
- 反射API
反射,是指一种能在运行时动态加载.分析类的能力.反射被广泛地用于那些需要在运行时检测或修改程序行为的程序中.这是一个相对高级的特性,使用反射技术应当具备相当的Java语言基础.我们可以通过反射机制让应 ...
- 获取字符宽度:并非自适应。coretext去计算
获取字符宽度:并非自适应.coretext去计算 UniChar ch = [ns_str characterAtIndex:0]; CGGlyph glyph = 0; CTFontGetGlyph ...