c# 验证类

C#-通用数据验证类 引入通用验证类命名控件using System.Text.RegularExpressions; 验证是否正整数验证是否Email验证身份证using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions;namespa 阅读详情

C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP

1namespace YongFa365.Validator
  2{
  3    using System;
  4    using System.Text.RegularExpressions;
  5    
  6    /**//// <summary>
  7    /// RegExp Soruce:   http://regexlib.com/DisplayPatterns.aspx
  8    /// Author:柳永法 yongfa365 http://www.yongfa365.com/ yongfa365@qq.com
  9    /// Intro:验证 网址,IP,邮箱,电话,手机,数字,英文,日期,身份证,邮编,
 10    /// 原则上是中国通用,因为各种场合不一样所以有特殊情况肯定要自己再手写,这里只能是提供一些通用的验证,追求太完美是不现实的。
 11    /// Version: 1.0
 12    /// PutTime: 2008-6-5
 13    /// LastModi:2008-6-5
 14    /// </summary>
 15    /// 

 16    public class Validator
 17    {
 18
 19        验证邮箱#region 验证邮箱
 20        /**//// <summary>
 21        /// 验证邮箱
 22        /// </summary>
 23        /// <param name="source"></param>
 24        /// <returns></returns>

 25        public static bool IsEmail(string source)
 26        {
 27            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);
 28        }

 29        public static bool HasEmail(string source)
 30        {
 31            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);
 32        }

 33        #endregion

 34
 35        验证网址#region 验证网址
 36        /**//// <summary>
 37        /// 验证网址
 38        /// </summary>
 39        /// <param name="source"></param>
 40        /// <returns></returns>

 41        public static bool IsUrl(string source)
 42        {
 43            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);
 44        }

 45        public static bool HasUrl(string source)
 46        {
 47            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);
 48        }

 49        #endregion

 50
 51        验证日期#region 验证日期
 52        /**//// <summary>
 53        /// 验证日期
 54        /// </summary>
 55        /// <param name="source"></param>
 56        /// <returns></returns>

 57        public static bool IsDateTime(string source)
 58        {
 59            try
 60            {
 61                DateTime time = Convert.ToDateTime(source);
 62                return true;
 63            }

 64            catch
 65            {
 66                return false;
 67            }

 68        }

 69        #endregion

 70
 71        验证手机号#region 验证手机号
 72        /**//// <summary>
 73        /// 验证手机号
 74        /// </summary>
 75        /// <param name="source"></param>
 76        /// <returns></returns>

 77        public static bool IsMobile(string source)
 78        {
 79            return Regex.IsMatch(source, @"^1[35]\d{9}$", RegexOptions.IgnoreCase);
 80        }

 81        public static bool HasMobile(string source)
 82        {
 83            return Regex.IsMatch(source, @"1[35]\d{9}", RegexOptions.IgnoreCase);
 84        }

 85        #endregion

 86
 87        验证IP#region 验证IP
 88        /**//// <summary>
 89        /// 验证IP
 90        /// </summary>
 91        /// <param name="source"></param>
 92        /// <returns></returns>

 93        public static bool IsIP(string source)
 94        {
 95            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);
 96        }

 97        public static bool HasIP(string source)
 98        {
 99            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);
100        }

101        #endregion

102
103        验证身份证是否有效#region 验证身份证是否有效
104        /**//// <summary>
105        /// 验证身份证是否有效
106        /// </summary>
107        /// <param name="Id"></param>
108        /// <returns></returns>

109        public static bool IsIDCard(string Id)
110        {
111            if (Id.Length == 18)
112            {
113                bool check = IsIDCard18(Id);
114                return check;
115            }

116            else if (Id.Length == 15)
117            {
118                bool check = IsIDCard15(Id);
119                return check;
120            }

121            else
122            {
123                return false;
124            }

125        }

126
127        public static bool IsIDCard18(string Id)
128        {
129            long n = 0;
130            if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
131            {
132                return false;//数字验证
133            }

134            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
135            if (address.IndexOf(Id.Remove(2)) == -1)
136            {
137                return false;//省份验证
138            }

139            string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
140            DateTime time = new DateTime();
141            if (DateTime.TryParse(birth, out time) == false)
142            {
143                return false;//生日验证
144            }

145            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
146            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
147            char[] Ai = Id.Remove(17).ToCharArray();
148            int sum = 0;
149            for (int i = 0; i < 17; i++)
150            {
151                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
152            }

153            int y = -1;
154            Math.DivRem(sum, 11, out y);
155            if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
156            {
157                return false;//校验码验证
158            }

159            return true;//符合GB11643-1999标准
160        }

161
162        public static bool IsIDCard15(string Id)
163        {
164            long n = 0;
165            if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
166            {
167                return false;//数字验证
168            }

169            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
170            if (address.IndexOf(Id.Remove(2)) == -1)
171            {
172                return false;//省份验证
173            }

174            string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
175            DateTime time = new DateTime();
176            if (DateTime.TryParse(birth, out time) == false)
177            {
178                return false;//生日验证
179            }

180            return true;//符合15位身份证标准
181        }

182        #endregion

183
184        是不是Int型的#region 是不是Int型的
185        /**//// <summary>
186        /// 是不是Int型的
187        /// </summary>
188        /// <param name="source"></param>
189        /// <returns></returns>

190        public static bool IsInt(string source)
191        {
192            Regex regex = new Regex(@"^(-){0,1}\d+$");
193            if (regex.Match(source).Success)
194            {
195                if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
196                {
197                    return false;
198                }

199                return true;
200            }

201            return false;
202        }

203        #endregion

204
205        看字符串的长度是不是在限定数之间 一个中文为两个字符#region 看字符串的长度是不是在限定数之间 一个中文为两个字符
206        /**//// <summary>
207        /// 看字符串的长度是不是在限定数之间 一个中文为两个字符
208        /// </summary>
209        /// <param name="source">字符串</param>
210        /// <param name="begin">大于等于</param>
211        /// <param name="end">小于等于</param>
212        /// <returns></returns>

213        public static bool IsLengthStr(string source, int begin, int end)
214        {
215            int length = Regex.Replace(source, @"[^\x00-\xff]", "OK").Length;
216            if ((length <= begin) && (length >= end))
217            {
218                return false;
219            }

220            return true;
221        }

222        #endregion

223
224        是不是中国电话,格式010-85849685#region 是不是中国电话,格式010-85849685
225        /**//// <summary>
226        /// 是不是中国电话,格式010-85849685
227        /// </summary>
228        /// <param name="source"></param>
229        /// <returns></returns>

230        public static bool IsTel(string source)
231        {
232            return Regex.IsMatch(source, @"^\d{3,4}-?\d{6,8}$", RegexOptions.IgnoreCase);
233        }

234        #endregion

235
236        邮政编码 6个数字#region 邮政编码 6个数字
237        /**//// <summary>
238        /// 邮政编码 6个数字
239        /// </summary>
240        /// <param name="source"></param>
241        /// <returns></returns>

242        public static bool IsPostCode(string source)
243        {
244            return Regex.IsMatch(source, @"^\d{6}$", RegexOptions.IgnoreCase);
245        }

246        #endregion

247
248        中文#region 中文
249        /**//// <summary>
250        /// 中文
251        /// </summary>
252        /// <param name="source"></param>
253        /// <returns></returns>

254        public static bool IsChinese(string source)
255        {
256            return Regex.IsMatch(source, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
257        }

258        public static bool hasChinese(string source)
259        {
260            return Regex.IsMatch(source, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
261        }

262        #endregion

263
264        验证是不是正常字符 字母,数字,下划线的组合#region 验证是不是正常字符 字母,数字,下划线的组合
265        /**//// <summary>
266        /// 验证是不是正常字符 字母,数字,下划线的组合
267        /// </summary>
268        /// <param name="source"></param>
269        /// <returns></returns>

270        public static bool IsNormalChar(string source)
271        {
272            return Regex.IsMatch(source, @"[\w\d_]+", RegexOptions.IgnoreCase);
273        }

274        #endregion

275
276    }

277}

C#中的正则表达式类集合:数据验证的利器 简介:正则表达式在C#中是文本处理的强大工具,用于执行匹配、查找、替换和提取等操作。本集合包含了多个C#类,专注于正则表达式的各种验证任务,如手机号码、邮箱地址、身份证号和邮政编码等格式验证。开发者可以通过使用这些类来快速实现输入数据的合法性检查,提高代码的可读性和可维护性。 阅读详情

相关推荐

【LDLTS解析】从重叠峰到精准参数:拉普拉斯深能级瞬态光谱如何重塑半导体缺陷分析

本文深入解析了拉普拉斯深能级瞬态光谱(LDLTS)技术如何革新半导体缺陷分析。针对传统DLTS难以分辨重叠峰的痛点,LDLTS通过拉普拉斯逆变换对高精度电容瞬态曲线进行数学解卷积,成功分离发射率相近的缺陷信号,实现从模糊宽峰到精准独立峰的转变。该技术不仅大幅提升了缺陷激活能和捕获截面等关键参数的提取精度,更推动分析范式从经验拟合转向第一性原理,为先进半导体工艺开发与良率提升提供了强大的精准诊断工具。

weixin_29224309的博客 272

C#验证码识别类-网上摘抄的

  C#验证码识别类-网上摘抄的,在网络上找的一篇关于验证码识别的类。代码如下。using System;using System.Collections.Generic;using System.Text;using System.Collections;using System.Drawing;using System.Drawing.Imaging;using System.Runt

Air的博客 1479

从 0 到 1:基于深度学习的图像识别系统开发实战

图像识别是指让计算机能够识别和理解图像中的内容,通常是通过计算机视觉技术来实现。深度学习,尤其是卷积神经网络(CNN),已经成为图像识别中最为流行和有效的技术。数据采集和预处理:获取并处理图像数据,使其适合于模型训练。模型训练:使用深度学习算法(如 CNN)训练模型,以学习图像中的特征。模型评估与优化:评估模型的性能,并通过调整超参数、优化算法等方法提高精度。模型部署与应用:将训练好的模型应用于实际的图像识别任务中。本文将带领你通过一个简单的图像分类问题,开发一个深度学习图像识别系统。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1217

C# GIF验证码类

 namespace Kissogram.Common.Security{     using System;     using System.IO;     using System.Web;     using System.Drawing;     //GIF验证码类     public class Validate     {         //设置最少4位验证码         p

感悟人生 1740

EPLAN电气设计精华 -模拟量传感器画法全解

本文介绍了EPLAN中模拟量压力传感器的标准画法要点,重点解析了电流型(4-20mA)、电压型(0-10V/0-5V)和特定应用型三种传感器的核心特点、典型符号、接线方式及应用场景。文章提供了EPLAN设备标识符示例,并分享了一个实际项目案例(XJC-Y01-20-4M传感器配合XJC-608T-F变送器和GSEE.GXLK-AI4S-I模块的应用),强调绘制前查阅技术资料的重要性。内容简明实用,有助于规范电气设计中的传感器绘制。

Qinmingyue13的博客 1179

niushop靶场漏洞查找-文件上传漏洞等(超详细)

发现报错,而且这个报错十分熟悉,与我们所学的sqli报错注入特别像。这里查询到后面的url有且仅有一个,目测估计是后台。上传的图片是包含一句话木马的图片。发现获取到了webshell。实战漏洞-niushop。

qq_59020256的博客 1726

C#验证类

usingSystem;2usingSystem.Text.RegularExpressions;34namespacebobomousecom.crm5{6/**//**//**////<summary>7///Regexlib的摘要说明。8///</summary>9publicclassRegexlib10{11...

205

Flutter蓝牙开发实战:flutter_blue_plus核心功能与实现原理剖析

本文深入剖析了Flutter蓝牙开发插件flutter_blue_plus的核心功能与实现原理。文章从源码解析入手,详细讲解了蓝牙状态管理、设备扫描、GATT连接、服务发现以及数据读写等关键流程的实现机制,并分享了实战中的性能调优与常见问题解决方案,帮助开发者高效构建跨平台蓝牙应用。

weixin_29014237的博客 443

[转载]C#识别验证码图片通用类

C#识别验证码图片通用类 本文转载而来,存档备用。 PS:作为C#新手的我,实在没有看懂怎么用它,怎么把本地图片传递给这个类。。。。。 道行不够啊,等道行深点了再回头来研究研究。 说明 封装后的类使用很简单,针对不同的验证码,相应继承修改某些方法,即可简单几句代码就可以实现图片识别了: GrayByPixels(); //灰度处理 GetPicValidByValue(128, 4); //得到有效空间 Bitmap[] pics = GetSplitPics(4, 1); //分割 string

qq_43256140的博客 1146

热力学仿真软件:Fluent_(13).燃烧与化学反应仿真

燃烧模型在热力学仿真软件中扮演着至关重要的角色。Fluent 提供了多种燃烧模型,包括层流预混燃烧模型、湍流预混燃烧模型、非预混燃烧模型、部分预混燃烧模型等。这些模型可以帮助用户模拟不同的燃烧过程,从而更好地理解燃烧现象和优化燃烧系统设计。

kkchenjj的博客 1252

【100%通过率】华为OD机试真题 C 实现【最多等和不相交连续子序列】【2022.11 Q4 新题】

给定一个数组,我们称其中连续的元素为连续子序列,称这些元素的和为连续子序列的和。数组中可能存在几组连续子序列,组内的连续子序列互不相交且有相同的和。求一组连续子序列,组内子序列的数目最多。输出这个数目。

MISAYAONE的博客 1万+

C#委托、类和事件的验证【C#】

C#委托、类和事件的验证 题目描述 程序由两部分组成,如下代码所示。第一部分定义了委托、类和事件。第二部分进行验证。 using System; namespace HelloWorldApplication { public delegate void DelegateRing(); public class Bell { public ...

C++的星空 488

2.2 保姆级教程:手把手带你构建第一个 LangGraph 应用

本文是一份保姆级教程,手把手教你构建第一个LangGraph应用——"智能工具助手"Agent。教程分为8个步骤:从环境准备到工具定义(使用Tavily搜索API和自定义计算器),再到定义图状态、创建决策节点和执行节点,最后连接工作流并编译运行。通过这个项目,你将学会如何让Agent智能选择工具(搜索或计算),掌握LangGraph开发的基本流程,为构建更复杂系统打下基础。关键点包括状态管理、节点设计和条件路由的实现。

yonggeit的博客 3115

C# 验证帮助类,验证器大全

哈喽!粉丝大大们,由于工作的原因,好久没有更新博客了,现在小码依旧在软件开发的前线,今天起个大早,不过时间紧迫,今天就给大家提供一些干货,C# 实现验证器,大家可以建一个验证器Validator类,将代码拷贝进去以备用。这篇文件没有啥技术好分析的,直接上代码(下篇文件我准备教大家封装一个SqlSugar底层以操作数据库,支持多个ORM、SQL、EF、LINQ等,欢迎关注更新!

很多问题不见得会出在你身上,但你亦需要想法解决问题,否则就会变成你的问题。 1558

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

C#中生成验证码的类

在很多网站中都会用到验证码的功能来防止非法注册用户,下面我就提供一个生成验证码的类。 代码如下: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using Sys...

weixin_34308389的博客 172

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

C#类 字符验证类 ip验证 邮箱验证等

using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace ZXF { class FK_CHECK { #region 判断对象是否为空 /// <summary> ...

网际游侠 347

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

一个C#验证类

前几天在项目中碰到的验证问题,由于不适合用验证控件,所以只好自己写了一个验证方法,贴上来。。。 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.We...

weixin_30480651的博客 278

用于验证码图片识别的类(C#源码)

最近写了几个网站的验证码图片自动识别程序,尽管每个网站的验证码图片都不相同,识别的方法有所差别。但写得多了,也总结出不少相同之处。今天抽空封装出一个基础类来,发现可以很好地重复利用,编

zhengxiuchen86的博客 786

C# 验证码生成类

using System; using System.Web; using System.Drawing; using System.Security.Cryptography; namespace DotNet.Utilities { /// /// 验证码类 /// public class Rand { #region 生成随机数

除却猩猩不是猿 431

C#分享辅助类:验证码图片生成类(VerifyCodeHandler)

【代码】C#分享辅助类:验证码图片生成类(VerifyCodeHandler)

lyp1215的专栏 719

c# 验证码类

using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Text; using System.Web; namespace Demo { /// <summa...

weixin_34258782的博客 253

C#验证码识别类的代码

如下内容内容是关于C#验证码识别类的内容。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; n...

lilacnia911的博客 872

简单C#验证类

一个比较方便的验证类封装 。如下:using System;using System.Text.RegularExpressions;namespace bobomousecom.crm{/// /// Regexlib 的摘要说明。/// public class Regexlib{public Re

sightboy的专栏 757
上一篇: C# 验证数字、小数正则表达式
下一篇: DataGridView 加载外部的ComBox
一点晨辉
博客等级 码龄18年 4粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值