


How to validate data helper class? Validate data helper class methods (code example)
The content of this article is to introduce how to implement a simple countdown effect of hours, minutes and seconds in js (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
using System; using System.Text.RegularExpressions; namespace ZMM.Core { /// <summary> /// 验证帮助类 /// </summary> public class ValidateHelper { //邮件正则表达式 private static Regex _emailregex = new Regex(@"^[a-z0-9]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$", RegexOptions.IgnoreCase); //手机号正则表达式 private static Regex _mobileregex = new Regex("^(13|15|17|18)[0-9]{9}$"); //固话号正则表达式 private static Regex _phoneregex = new Regex(@"^(\d{3,4}-?)?\d{7,8}$"); //IP正则表达式 private static Regex _ipregex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"); //日期正则表达式 private static Regex _dateregex = new Regex(@"(\d{4})-(\d{1,2})-(\d{1,2})"); //数值(包括整数和小数)正则表达式 private static Regex _numericregex = new Regex(@"^[-]?[0-9]+(\.[0-9]+)?$"); //邮政编码正则表达式 private static Regex _zipcoderegex = new Regex(@"^\d{6}$"); /// <summary> /// 是否为邮箱名 /// </summary> public static bool IsEmail(string s) { if (string.IsNullOrEmpty(s)) return true; return _emailregex.IsMatch(s); } /// <summary> /// 是否为手机号 /// </summary> public static bool IsMobile(string s) { if (string.IsNullOrEmpty(s)) return true; return _mobileregex.IsMatch(s); } /// <summary> /// 是否为固话号 /// </summary> public static bool IsPhone(string s) { if (string.IsNullOrEmpty(s)) return true; return _phoneregex.IsMatch(s); } /// <summary> /// 是否为IP /// </summary> public static bool IsIP(string s) { return _ipregex.IsMatch(s); } /// <summary> /// 是否是身份证号 /// </summary> public static bool IsIdCard(string id) { if (string.IsNullOrEmpty(id)) return true; if (id.Length == 18) return CheckIDCard18(id); else if (id.Length == 15) return CheckIDCard15(id); else return false; } /// <summary> /// 是否为18位身份证号 /// </summary> private static bool CheckIDCard18(string Id) { long n = 0; 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) return false;//数字验证 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if (address.IndexOf(Id.Remove(2)) == -1) return false;//省份验证 string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-"); 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(17).ToCharArray(); int sum = 0; for (int i = 0; i < 17; i++) sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString()); int y = -1; Math.DivRem(sum, 11, out y); if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower()) return false;//校验码验证 return true;//符合GB11643-1999标准 } /// <summary> /// 是否为15位身份证号 /// </summary> private static bool CheckIDCard15(string Id) { long n = 0; if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14)) return false;//数字验证 string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if (address.IndexOf(Id.Remove(2)) == -1) return false;//省份验证 string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-"); DateTime time = new DateTime(); if (DateTime.TryParse(birth, out time) == false) return false;//生日验证 return true;//符合15位身份证标准 } /// <summary> /// 是否为日期 /// </summary> public static bool IsDate(string s) { return _dateregex.IsMatch(s); } /// <summary> /// 是否是数值(包括整数和小数) /// </summary> public static bool IsNumeric(string numericStr) { return _numericregex.IsMatch(numericStr); } /// <summary> /// 是否为邮政编码 /// </summary> public static bool IsZipCode(string s) { if (string.IsNullOrEmpty(s)) return true; return _zipcoderegex.IsMatch(s); } /// <summary> /// 是否是图片文件名 /// </summary> /// <returns> </returns> public static bool IsImgFileName(string fileName) { if (fileName.IndexOf(".") == -1) return false; string tempFileName = fileName.Trim().ToLower(); string extension = tempFileName.Substring(tempFileName.LastIndexOf(".")); return extension == ".png" || extension == ".bmp" || extension == ".jpg" || extension == ".jpeg" || extension == ".gif"; } /// <summary> /// 是否是文件名word /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static bool IsFile(string fileName) { if (fileName.IndexOf(".") == -1) return false; string tempFileName = fileName.Trim().ToLower(); string extension = tempFileName.Substring(tempFileName.LastIndexOf(".")); return extension == ".doc" || extension == ".docx" || extension == ".xls" || extension == ".xlsx" || extension == ".txt"; } /// <summary> /// 判断一个ip是否在另一个ip内 /// </summary> /// <param name="sourceIP">检测ip</param> /// <param name="targetIP">匹配ip</param> /// <returns></returns> public static bool InIP(string sourceIP, string targetIP) { if (string.IsNullOrEmpty(sourceIP) || string.IsNullOrEmpty(targetIP)) return false; string[] sourceIPBlockList = StringHelper.SplitString(sourceIP, @"."); string[] targetIPBlockList = StringHelper.SplitString(targetIP, @"."); int sourceIPBlockListLength = sourceIPBlockList.Length; for (int i = 0; i < sourceIPBlockListLength; i++) { if (targetIPBlockList[i] == "*") return true; if (sourceIPBlockList[i] != targetIPBlockList[i]) { return false; } else { if (i == 3) return true; } } return false; } /// <summary> /// 判断一个ip是否在另一个ip内 /// </summary> /// <param name="sourceIP">检测ip</param> /// <param name="targetIPList">匹配ip列表</param> /// <returns></returns> public static bool InIPList(string sourceIP, string[] targetIPList) { if (targetIPList != null && targetIPList.Length > 0) { foreach (string targetIP in targetIPList) { if (InIP(sourceIP, targetIP)) return true; } } return false; } /// <summary> /// 判断一个ip是否在另一个ip内 /// </summary> /// <param name="sourceIP">检测ip</param> /// <param name="targetIPStr">匹配ip</param> /// <returns></returns> public static bool InIPList(string sourceIP, string targetIPStr) { string[] targetIPList = StringHelper.SplitString(targetIPStr, "\n"); return InIPList(sourceIP, targetIPList); } /// <summary> /// 判断当前时间是否在指定的时间段内 /// </summary> /// <param name="periodList">指定时间段</param> /// <param name="liePeriod">所处时间段</param> /// <returns></returns> public static bool BetweenPeriod(string[] periodList, out string liePeriod) { if (periodList != null && periodList.Length > 0) { DateTime startTime; DateTime endTime; DateTime nowTime = DateTime.Now; DateTime nowDate = nowTime.Date; foreach (string period in periodList) { int index = period.IndexOf("-"); startTime = TypeHelper.StringToDateTime(period.Substring(0, index)); endTime = TypeHelper.StringToDateTime(period.Substring(index + 1)); if (startTime < endTime) { if (nowTime > startTime && nowTime < endTime) { liePeriod = period; return true; } } else { if ((nowTime > startTime && nowTime < nowDate.AddDays(1)) || (nowTime < endTime)) { liePeriod = period; return true; } } } } liePeriod = string.Empty; return false; } /// <summary> /// 判断当前时间是否在指定的时间段内 /// </summary> /// <param name="periodStr">指定时间段</param> /// <param name="liePeriod">所处时间段</param> /// <returns></returns> public static bool BetweenPeriod(string periodStr, out string liePeriod) { string[] periodList = StringHelper.SplitString(periodStr, "\n"); return BetweenPeriod(periodList, out liePeriod); } /// <summary> /// 判断当前时间是否在指定的时间段内 /// </summary> /// <param name="periodList">指定时间段</param> /// <returns></returns> public static bool BetweenPeriod(string periodList) { string liePeriod = string.Empty; return BetweenPeriod(periodList, out liePeriod); } /// <summary> /// 是否是数值(包括整数和小数) /// </summary> public static bool IsNumericArray(string[] numericStrList) { if (numericStrList != null && numericStrList.Length > 0) { foreach (string numberStr in numericStrList) { if (!IsNumeric(numberStr)) return false; } return true; } return false; } /// <summary> /// 是否是数值(包括整数和小数) /// </summary> public static bool IsNumericRule(string numericRuleStr, string splitChar) { return IsNumericArray(StringHelper.SplitString(numericRuleStr, splitChar)); } /// <summary> /// 是否是数值(包括整数和小数) /// </summary> public static bool IsNumericRule(string numericRuleStr) { return IsNumericRule(numericRuleStr, ","); } } }
The above is the detailed content of How to validate data helper class? Validate data helper class methods (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

If you are a .NET developer, you must be aware of the importance of optimizing functionality and performance in delivering high-quality software. By making expert use of the provided resources and reducing website load times, you not only create a pleasant experience for your users but also reduce infrastructure costs.

In terms of high-concurrency request processing, .NETASP.NETCoreWebAPI performs better than JavaSpringMVC. The reasons include: AOT early compilation, which reduces startup time; more refined memory management, where developers are responsible for allocating and releasing object memory.

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.
