c# 常见验证邮箱、电话号码、日期等格式
- #region 验证邮箱验证邮箱
- /**//// <summary>
- /// 验证邮箱
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsEmail(string source)
- {
- return Regex.IsMatch(source, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.IgnoreCase);
- }
- public static bool HasEmail(string source)
- {
- return Regex.IsMatch(source, @"[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})", RegexOptions.IgnoreCase);
- }
- #endregion
- #region 验证网址
- /**//// <summary>
- /// 验证网址
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsUrl(string source)
- {
- return Regex.IsMatch(source, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?$", RegexOptions.IgnoreCase);
- }
- public static bool HasUrl(string source)
- {
- return Regex.IsMatch(source, @"(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?", RegexOptions.IgnoreCase);
- }
- #endregion
- #region 验证日期
- /**//// <summary>
- /// 验证日期
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsDateTime(string source)
- {
- try
- {
- DateTime time = Convert.ToDateTime(source);
- return true;
- }
- catch
- {
- return false;
- }
- }
- #endregion
- #region 验证手机号
- /**//// <summary>
- /// 验证手机号
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsMobile(string source)
- {
- return Regex.IsMatch(source, @"^1[35]\d{9}$", RegexOptions.IgnoreCase);
- }
- public static bool HasMobile(string source)
- {
- return Regex.IsMatch(source, @"1[35]\d{9}", RegexOptions.IgnoreCase);
- }
- #endregion
- #region 验证IP
- /**//// <summary>
- /// 验证IP
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsIP(string source)
- {
- return Regex.IsMatch(source, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$", RegexOptions.IgnoreCase);
- }
- public static bool HasIP(string source)
- {
- return Regex.IsMatch(source, @"(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])", RegexOptions.IgnoreCase);
- }
- #endregion
- #region 验证身份证是否有效
- /**//// <summary>
- /// 验证身份证是否有效
- /// </summary>
- /// <param name="Id"></param>
- /// <returns></returns>
- public static bool IsIDCard(string Id)
- {
- if (Id.Length == )
- {
- bool check = IsIDCard18(Id);
- return check;
- }
- else if (Id.Length == )
- {
- bool check = IsIDCard15(Id);
- return check;
- }
- else
- {
- return false;
- }
- }
- public static bool IsIDCard18(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(, "-");
- DateTime time = new DateTime();
- if (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标准
- }
- public static bool IsIDCard15(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(, "-");
- DateTime time = new DateTime();
- if (DateTime.TryParse(birth, out time) == false)
- {
- return false;//生日验证
- }
- return true;//符合15位身份证标准
- }
- #endregion
- #region 是不是Int型的
- /**//// <summary>
- /// 是不是Int型的
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsInt(string source)
- {
- Regex regex = new Regex(@"^(-){0,1}\d+$");
- if (regex.Match(source).Success)
- {
- if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
- {
- return false;
- }
- return true;
- }
- return false;
- }
- #endregion
- #region 看字符串的长度是不是在限定数之间 一个中文为两个字符
- /**//// <summary>
- /// 看字符串的长度是不是在限定数之间 一个中文为两个字符
- /// </summary>
- /// <param name="source">字符串</param>
- /// <param name="begin">大于等于</param>
- /// <param name="end">小于等于</param>
- /// <returns></returns>
- public static bool IsLengthStr(string source, int begin, int end)
- {
- int length = Regex.Replace(source, @"[^\x00-\xff]", "OK").Length;
- if ((length <= begin) && (length >= end))
- {
- return false;
- }
- return true;
- }
- #endregion
- #region 是不是中国电话,格式010-85849685
- /**//// <summary>
- /// 是不是中国电话,格式010-85849685
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsTel(string source)
- {
- return Regex.IsMatch(source, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
- }
- #endregion
- #region 邮政编码 6个数字
- /**//// <summary>
- /// 邮政编码 6个数字
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsPostCode(string source)
- {
- return Regex.IsMatch(source, @"^\d{6}$", RegexOptions.IgnoreCase);
- }
- #endregion
- #region 中文
- /**//// <summary>
- /// 中文
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool IsChinese(string source)
- {
- return Regex.IsMatch(source, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
- }
- public static bool hasChinese(string source)
- {
- return Regex.IsMatch(source, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
- }
- #endregion
- #region 验证是不是正常字符 字母,数字,下划线的组合
- /**//// <summary>
- /// 验证是不是正常字符 字母,数字,下划线的组合
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static bool I#endregionsNormalChar(string source)
- {
- return Regex.IsMatch(source, @"[\w\d_]+", RegexOptions.IgnoreCase);
- }
- #endregion
c# 常见验证邮箱、电话号码、日期等格式的更多相关文章
- java常见验证邮箱、电话号码、日期等格式
package besttone.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 90%的验证 ...
- js正则表单验证汇总,邮箱验证,日期验证,电话号码验证,url验证,信用卡验证,qq验证
本文主要汇总各种正则验证,很多都是转载,本人也会尽可能验证准确性,如有错误欢迎留言 //trim()方法在有些浏览器中不兼容,最好自己重写一下 String.prototype.trim=functi ...
- ★★★【卡法 常用js库】: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度
[卡法 常用js库]: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度 // +---------------------- ...
- C#验证邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP类等常用函数封装
#region 验证邮箱验证邮箱 /**//// <summary> /// 验证邮箱 /// </summary> /// <param name="sour ...
- JS验证邮箱格式是否正确的代码
验证邮箱格式是否正确的方法有很多,接下来为大家介绍下使用js是如何做到的 复制代码代码如下: /* *验证邮箱格式是否正确 *参数strEmail,需要验证的邮箱 */ www.jbxue.co ...
- jquery+正則表達式验证邮箱格式的样例
js: $("#email").blur(function(){ //获取id相应的元素的值,去掉其左右的空格 var email = $.trim($('#email').val ...
- Android 使用正则表达式验证邮箱格式是否正确
/** * 验证邮箱格式是否正确 */ public boolean emailValidation(String email) { String regex = "\\w+([-+.]\\ ...
- JS验证邮箱格式是否正确 实例代码
如何用js验证邮箱格式是否正确?分享一个例子.代码: /* *验证邮箱格式是否正确 *参数strEmail,需要验证的邮箱 */ function chkEmail(strEmail) { if (! ...
- filter_var() 验证邮箱、ip、url的格式 php
验证邮箱格式的正确与否:你的第一解决方案是什么呢? 不管你们怎么思考的:反正我首先想到的就是字符串查找看是否有@符号: 但是对于结尾的.com或者.net 亦或者.cn等等越来越多的域名验证感觉棘手: ...
随机推荐
- 2、java内存间交互操作
关于主内存与工作内存之间具体的交互协议,即一个变量如何从主内存拷贝到工作内存,如何从工作内存同步回主内存之类的实现细节,java内存模型中定义了8种操作来完成,虚拟机实现时必须保证这8种操作都是原子的 ...
- FontAwesome 图标 class="fa fa-home"
本文转自:http://www.yeahzan.com/fa/facss.html 引用方式: class="fa fa-home" 注意:每个图标的引用都必须要添加 fa fa- ...
- node.js压缩和解压缩
推荐一个极其简单.及其好用的node.js的压缩和解压缩类库 compressing 支持格式: tar.gzip.tgz.zip 以zip为例,tar,tgz和gzip与zip相同. 压缩文件: ...
- shell命令修改文件内容
有个 test.txt 文件内容为 hello tom,现在修改成 hello jerry,并保存到test2.txt sed 's/tom/jerry/g' test.txt >test2. ...
- (三)HTML中的列表标签、框架集及表单标签
一.HTML的列表标签 在网页中,经常可以看到,有的内容排列如同word里面的项目编号,这就是HTML的无序排列和有序排列起到的作用.. HTML之无序排列:<ul></ul> ...
- A bug about RecipientEditTextView
- Steps to reproduce the problem. Pre-condition:the threshold of the RecipientEditTextView is set to ...
- 菜鸟学配置vim
看啥都不会的菜鸟怎么进行vim配置 如果你想让你的vim和VS差不多你一定需要这个网址 http://www.open-open.com/lib/view/open1429884437588.html ...
- nodejs进阶(7)—async异步流程控制
Async介绍 Async是一个流程控制工具包,提供了直接而强大的异步功能.基于Javascript为Node.js设计,同时也可以直接在浏览器中使用. Async提供了大约20个函数,包括常用的 m ...
- gsap
TweenMax借助于css,轻量级的js库 下面是简单的demo <!DOCTYPE html> <html lang="en"> <head> ...
- HTML字符实体名称/实体编号
字符实体对英文的大小写敏感! 字符实体一: 显示结果 描述 实体名称 实体编号 空格 < 小于号 < < > 大于号 > > & 和号 & ...