1. #region 验证邮箱验证邮箱
  2. /**//// <summary>
  3. /// 验证邮箱
  4. /// </summary>
  5. /// <param name="source"></param>
  6. /// <returns></returns>
  7. public static bool IsEmail(string source)
  8. {
  9. return Regex.IsMatch(source, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.IgnoreCase);
  10. }
  11. public static bool HasEmail(string source)
  12. {
  13. 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);
  14. }
  15. #endregion
  16. #region 验证网址
  17. /**//// <summary>
  18. /// 验证网址
  19. /// </summary>
  20. /// <param name="source"></param>
  21. /// <returns></returns>
  22. public static bool IsUrl(string source)
  23. {
  24. 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\&amp;%_\./-~-]*)?$", RegexOptions.IgnoreCase);
  25. }
  26. public static bool HasUrl(string source)
  27. {
  28. 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\&amp;%_\./-~-]*)?", RegexOptions.IgnoreCase);
  29. }
  30. #endregion
  31.  
  32. #region 验证日期
  33. /**//// <summary>
  34. /// 验证日期
  35. /// </summary>
  36. /// <param name="source"></param>
  37. /// <returns></returns>
  38. public static bool IsDateTime(string source)
  39. {
  40. try
  41. {
  42. DateTime time = Convert.ToDateTime(source);
  43. return true;
  44. }
  45. catch
  46. {
  47. return false;
  48. }
  49. }
  50. #endregion
  51.  
  52. #region 验证手机号
  53. /**//// <summary>
  54. /// 验证手机号
  55. /// </summary>
  56. /// <param name="source"></param>
  57. /// <returns></returns>
  58. public static bool IsMobile(string source)
  59. {
  60. return Regex.IsMatch(source, @"^1[35]\d{9}$", RegexOptions.IgnoreCase);
  61. }
  62. public static bool HasMobile(string source)
  63. {
  64. return Regex.IsMatch(source, @"1[35]\d{9}", RegexOptions.IgnoreCase);
  65. }
  66. #endregion
  67.  
  68. #region 验证IP
  69. /**//// <summary>
  70. /// 验证IP
  71. /// </summary>
  72. /// <param name="source"></param>
  73. /// <returns></returns>
  74. public static bool IsIP(string source)
  75. {
  76. 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);
  77. }
  78. public static bool HasIP(string source)
  79. {
  80. 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);
  81. }
  82. #endregion
  83. #region 验证身份证是否有效
  84. /**//// <summary>
  85. /// 验证身份证是否有效
  86. /// </summary>
  87. /// <param name="Id"></param>
  88. /// <returns></returns>
  89. public static bool IsIDCard(string Id)
  90. {
  91. if (Id.Length == )
  92. {
  93. bool check = IsIDCard18(Id);
  94. return check;
  95. }
  96. else if (Id.Length == )
  97. {
  98. bool check = IsIDCard15(Id);
  99. return check;
  100. }
  101. else
  102. {
  103. return false;
  104. }
  105. }
  106.  
  107. public static bool IsIDCard18(string Id)
  108. {
  109. long n = ;
  110. if (long.TryParse(Id.Remove(), out n) == false || n < Math.Pow(, ) || long.TryParse(Id.Replace('x', '').Replace('X', ''), out n) == false)
  111. {
  112. return false;//数字验证
  113. }
  114. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  115. if (address.IndexOf(Id.Remove()) == -)
  116. {
  117. return false;//省份验证
  118. }
  119. string birth = Id.Substring(, ).Insert(, "-").Insert(, "-");
  120. DateTime time = new DateTime();
  121. if (DateTime.TryParse(birth, out time) == false)
  122. {
  123. return false;//生日验证
  124. }
  125. string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
  126. string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
  127. char[] Ai = Id.Remove().ToCharArray();
  128. int sum = ;
  129. for (int i = ; i < ; i++)
  130. {
  131. sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
  132. }
  133. int y = -;
  134. Math.DivRem(sum, , out y);
  135. if (arrVarifyCode[y] != Id.Substring(, ).ToLower())
  136. {
  137. return false;//校验码验证
  138. }
  139. return true;//符合GB11643-1999标准
  140. }
  141.  
  142. public static bool IsIDCard15(string Id)
  143. {
  144. long n = ;
  145. if (long.TryParse(Id, out n) == false || n < Math.Pow(, ))
  146. {
  147. return false;//数字验证
  148. }
  149. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  150. if (address.IndexOf(Id.Remove()) == -)
  151. {
  152. return false;//省份验证
  153. }
  154. string birth = Id.Substring(, ).Insert(, "-").Insert(, "-");
  155. DateTime time = new DateTime();
  156. if (DateTime.TryParse(birth, out time) == false)
  157. {
  158. return false;//生日验证
  159. }
  160. return true;//符合15位身份证标准
  161. }
  162. #endregion
  163.  
  164. #region 是不是Int型的
  165. /**//// <summary>
  166. /// 是不是Int型的
  167. /// </summary>
  168. /// <param name="source"></param>
  169. /// <returns></returns>
  170. public static bool IsInt(string source)
  171. {
  172. Regex regex = new Regex(@"^(-){0,1}\d+$");
  173. if (regex.Match(source).Success)
  174. {
  175. if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
  176. {
  177. return false;
  178. }
  179. return true;
  180. }
  181. return false;
  182. }
  183. #endregion
  184.  
  185. #region 看字符串的长度是不是在限定数之间 一个中文为两个字符
  186. /**//// <summary>
  187. /// 看字符串的长度是不是在限定数之间 一个中文为两个字符
  188. /// </summary>
  189. /// <param name="source">字符串</param>
  190. /// <param name="begin">大于等于</param>
  191. /// <param name="end">小于等于</param>
  192. /// <returns></returns>
  193. public static bool IsLengthStr(string source, int begin, int end)
  194. {
  195. int length = Regex.Replace(source, @"[^\x00-\xff]", "OK").Length;
  196. if ((length <= begin) && (length >= end))
  197. {
  198. return false;
  199. }
  200. return true;
  201. }
  202. #endregion
  203.  
  204. #region 是不是中国电话,格式010-85849685
  205. /**//// <summary>
  206. /// 是不是中国电话,格式010-85849685
  207. /// </summary>
  208. /// <param name="source"></param>
  209. /// <returns></returns>
  210. public static bool IsTel(string source)
  211. {
  212. return Regex.IsMatch(source, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
  213. }
  214. #endregion
  215.  
  216. #region 邮政编码 6个数字
  217. /**//// <summary>
  218. /// 邮政编码 6个数字
  219. /// </summary>
  220. /// <param name="source"></param>
  221. /// <returns></returns>
  222. public static bool IsPostCode(string source)
  223. {
  224. return Regex.IsMatch(source, @"^\d{6}$", RegexOptions.IgnoreCase);
  225. }
  226. #endregion
  227. #region 中文
  228. /**//// <summary>
  229. /// 中文
  230. /// </summary>
  231. /// <param name="source"></param>
  232. /// <returns></returns>
  233. public static bool IsChinese(string source)
  234. {
  235. return Regex.IsMatch(source, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
  236. }
  237. public static bool hasChinese(string source)
  238. {
  239. return Regex.IsMatch(source, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
  240. }
  241. #endregion
  242.  
  243. #region 验证是不是正常字符 字母,数字,下划线的组合
  244. /**//// <summary>
  245. /// 验证是不是正常字符 字母,数字,下划线的组合
  246. /// </summary>
  247. /// <param name="source"></param>
  248. /// <returns></returns>
  249. public static bool I#endregionsNormalChar(string source)
  250. {
  251. return Regex.IsMatch(source, @"[\w\d_]+", RegexOptions.IgnoreCase);
  252. }
  253. #endregion

c# 常见验证邮箱、电话号码、日期等格式的更多相关文章

  1. java常见验证邮箱、电话号码、日期等格式

    package besttone.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 90%的验证 ...

  2. js正则表单验证汇总,邮箱验证,日期验证,电话号码验证,url验证,信用卡验证,qq验证

    本文主要汇总各种正则验证,很多都是转载,本人也会尽可能验证准确性,如有错误欢迎留言 //trim()方法在有些浏览器中不兼容,最好自己重写一下 String.prototype.trim=functi ...

  3. ★★★【卡法 常用js库】: js汇合 表单验证 cookie设置 日期格式 电话手机号码 email 整数 小数 金额 检查参数长度

    [卡法 常用js库]: js汇合 表单验证  cookie设置  日期格式  电话手机号码  email  整数  小数  金额   检查参数长度 // +---------------------- ...

  4. C#验证邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP类等常用函数封装

    #region 验证邮箱验证邮箱 /**//// <summary> /// 验证邮箱 /// </summary> /// <param name="sour ...

  5. JS验证邮箱格式是否正确的代码

    验证邮箱格式是否正确的方法有很多,接下来为大家介绍下使用js是如何做到的 复制代码代码如下: /*  *验证邮箱格式是否正确  *参数strEmail,需要验证的邮箱  */ www.jbxue.co ...

  6. jquery+正則表達式验证邮箱格式的样例

    js: $("#email").blur(function(){ //获取id相应的元素的值,去掉其左右的空格 var email = $.trim($('#email').val ...

  7. Android 使用正则表达式验证邮箱格式是否正确

    /** * 验证邮箱格式是否正确 */ public boolean emailValidation(String email) { String regex = "\\w+([-+.]\\ ...

  8. JS验证邮箱格式是否正确 实例代码

    如何用js验证邮箱格式是否正确?分享一个例子.代码: /* *验证邮箱格式是否正确 *参数strEmail,需要验证的邮箱 */ function chkEmail(strEmail) { if (! ...

  9. filter_var() 验证邮箱、ip、url的格式 php

    验证邮箱格式的正确与否:你的第一解决方案是什么呢? 不管你们怎么思考的:反正我首先想到的就是字符串查找看是否有@符号: 但是对于结尾的.com或者.net 亦或者.cn等等越来越多的域名验证感觉棘手: ...

随机推荐

  1. 2、java内存间交互操作

    关于主内存与工作内存之间具体的交互协议,即一个变量如何从主内存拷贝到工作内存,如何从工作内存同步回主内存之类的实现细节,java内存模型中定义了8种操作来完成,虚拟机实现时必须保证这8种操作都是原子的 ...

  2. FontAwesome 图标 class="fa fa-home"

    本文转自:http://www.yeahzan.com/fa/facss.html 引用方式: class="fa fa-home" 注意:每个图标的引用都必须要添加 fa fa- ...

  3. node.js压缩和解压缩

    推荐一个极其简单.及其好用的node.js的压缩和解压缩类库  compressing 支持格式: tar.gzip.tgz.zip 以zip为例,tar,tgz和gzip与zip相同. 压缩文件: ...

  4. shell命令修改文件内容

    有个 test.txt 文件内容为  hello tom,现在修改成 hello jerry,并保存到test2.txt sed 's/tom/jerry/g' test.txt >test2. ...

  5. (三)HTML中的列表标签、框架集及表单标签

    一.HTML的列表标签 在网页中,经常可以看到,有的内容排列如同word里面的项目编号,这就是HTML的无序排列和有序排列起到的作用.. HTML之无序排列:<ul></ul> ...

  6. A bug about RecipientEditTextView

    - Steps to reproduce the problem. Pre-condition:the threshold of the RecipientEditTextView is set to ...

  7. 菜鸟学配置vim

    看啥都不会的菜鸟怎么进行vim配置 如果你想让你的vim和VS差不多你一定需要这个网址 http://www.open-open.com/lib/view/open1429884437588.html ...

  8. nodejs进阶(7)—async异步流程控制

    Async介绍 Async是一个流程控制工具包,提供了直接而强大的异步功能.基于Javascript为Node.js设计,同时也可以直接在浏览器中使用. Async提供了大约20个函数,包括常用的 m ...

  9. gsap

    TweenMax借助于css,轻量级的js库 下面是简单的demo <!DOCTYPE html> <html lang="en"> <head> ...

  10. HTML字符实体名称/实体编号

    字符实体对英文的大小写敏感! 字符实体一: 显示结果 描述 实体名称 实体编号   空格     < 小于号 < < > 大于号 > > & 和号 & ...