Class Validator
Namespace: Tool.Utils
Assembly: Tool.Net.dll
相关正则类(包含常用的正则表达式)高达65种供大家参考学习,使用。
public class ValidatorInheritance
Inherited Members
object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()
Extension Methods
ObjectExtension.Add<T>(object, object, object), ObjectExtension.CopyEntity(object, object, params string[]), ObjectExtension.EntityToJson(object), ObjectExtension.EntityToJson(object, bool), ObjectExtension.EntityToJson(object, bool, string), DictionaryExtension.GetDictionary(object), TypeExtension.GetFieldKey(object, string, out bool), TypeExtension.GetFieldKey<T>(object, string, out bool), TypeExtension.GetFieldKey(object, Type, string, out bool), ObjectExtension.GetIntPtr(object), ObjectExtension.GetIntPtrInt(object), TypeExtension.GetPropertieFind(object, string, bool), TypeExtension.GetProperties(object), TypeExtension.GetPropertyKey(object, string, out bool), TypeExtension.GetPropertyKey<T>(object, string, out bool), TypeExtension.GetPropertyKey(object, Type, string, out bool), TypeExtension.GetValue(object, PropertyDescriptor), TypeExtension.GetValue(object, string, bool), TypeExtension.GetValue(object, string), ObjectExtension.Read<T>(object, object, int, int), ObjectExtension.Read<T>(object, int, object, int, int), DictionaryExtension.SetDictionary(object, IDictionary<string, object>), TypeExtension.SetFieldKey(object, string, object), TypeExtension.SetFieldKey<T>(object, string, object), TypeExtension.SetFieldKey(object, Type, string, object), TypeExtension.SetPropertyKey(object, string, object), TypeExtension.SetPropertyKey<T>(object, string, object), TypeExtension.SetPropertyKey(object, Type, string, object), TypeExtension.SetValue(object, PropertyDescriptor, object), TypeExtension.SetValue(object, string, object, bool), TypeExtension.SetValue(object, string, object), ObjectExtension.ToBase64String(object), ObjectExtension.ToBytes(object), ObjectExtension.ToBytes(object, out Type), DictionaryExtension.ToDictionary(object), DictionaryExtension.ToDictionary<T>(object), DictionaryExtension.ToIDictionary(object), DictionaryExtension.ToIDictionary<T>(object), ObjectExtension.ToJson(object), ObjectExtension.ToJson(object, JsonSerializerOptions), ObjectExtension.ToJsonWeb(object), ObjectExtension.ToJsonWeb(object, Action<JsonSerializerOptions>), ObjectExtension.ToTryVar<T>(object, T), ObjectExtension.ToVar<T>(object), ObjectExtension.ToVar(object, Type, bool), ObjectExtension.ToVar(object, string), ObjectExtension.ToXml(object)
Remarks
代码由逆血提供支持
Fields
reg
数字
public const string reg = "^[0-9]*$"Field Value
reg1
n位的数字
public const string reg1 = "^\\d{n}$"Field Value
reg10
非零的正整数
public const string reg10 = "^[1-9]\\d*$"Field Value
reg10_1
非零的正整数
public const string reg10_1 = "^([1-9][0-9]*){1,3}$"Field Value
reg10_2
非零的正整数
public const string reg10_2 = "^\\+?[1-9][0-9]*$"Field Value
reg11
非零的负整数
public const string reg11 = "^\\-[1-9][]0-9″*$"Field Value
reg11_1
非零的负整数
public const string reg11_1 = "^-[1-9]\\d*$"Field Value
reg12
非负整数
public const string reg12 = "^\\d+$"Field Value
reg12_1
非负整数
public const string reg12_1 = "^[1-9]\\d*|0$"Field Value
reg13
非正整数
public const string reg13 = "^-[1-9]\\d*|0$"Field Value
reg13_1
非正整数
public const string reg13_1 = "^((-\\d+)|(0+))$"Field Value
reg14
非负浮点数
public const string reg14 = "^\\d+(\\.\\d+)?$"Field Value
reg14_1
非负浮点数
public const string reg14_1 = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$"Field Value
reg15
非正浮点数
public const string reg15 = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"Field Value
reg15_1
非正浮点数
public const string reg15_1 = "^(-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*))|0?\\.0+|0$"Field Value
reg16
正浮点数
public const string reg16 = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$"Field Value
reg16_1
正浮点数
public const string reg16_1 = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"Field Value
reg17
负浮点数
public const string reg17 = "^-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*)$"Field Value
reg17_1
负浮点数
public const string reg17_1 = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"Field Value
reg18
浮点数
public const string reg18 = "^(-?\\d+)(\\.\\d+)?$"Field Value
reg18_1
浮点数
public const string reg18_1 = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$"Field Value
reg19
汉字
public const string reg19 = "^[\\u4e00-\\u9fa5]{0,}$"Field Value
reg2
至少n位的数字
public const string reg2 = "^\\d{n,}$"Field Value
reg20
英文和数字
public const string reg20 = "^[A-Za-z0-9]+$"Field Value
reg20_1
英文和数字
public const string reg20_1 = "^[A-Za-z0-9]{4,40}$"Field Value
reg21
长度为3-20的所有字符
public const string reg21 = "^.{3,20}$"Field Value
reg22
由26个英文字母组成的字符串
public const string reg22 = "^[A-Za-z]+$"Field Value
reg23
由26个大写英文字母组成的字符串
public const string reg23 = "^[A-Z]+$"Field Value
reg24
由26个小写英文字母组成的字符串
public const string reg24 = "^[a-z]+$"Field Value
reg25
由数字和26个英文字母组成的字符串
public const string reg25 = "^[A-Za-z0-9]+$"Field Value
reg26
由数字、26个英文字母或者下划线组成的字符串
public const string reg26 = "^\\w+$"Field Value
reg26_1
由数字、26个英文字母或者下划线组成的字符串
public const string reg26_1 = "^\\w{3,20}$"Field Value
reg27
中文、英文、数字包括下划线
public const string reg27 = "^[\\u4E00-\\u9FA5A-Za-z0-9_]+$"Field Value
reg28
中文、英文、数字但不包括下划线等符号
public const string reg28 = "^[\\u4E00-\\u9FA5A-Za-z0-9]+$"Field Value
reg28_1
中文、英文、数字但不包括下划线等符号
public const string reg28_1 = "^[\\u4E00-\\u9FA5A-Za-z0-9]{2,20}$"Field Value
reg29
可以输入含有^%’,;=?$\”等字符
public const string reg29 = "[^%&’,;=?$\\x22]+"Field Value
reg3
m-n位的数字
public const string reg3 = "^\\d{m,n}$"Field Value
reg30
禁止输入含有~的字符
public const string reg30 = "[^~\\x22]+"Field Value
reg31
Email地址
public const string reg31 = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"Field Value
reg32
域名
public const string reg32 = "[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?"Field Value
reg33
InternetURL
public const string reg33 = "[a-zA-z]+://[^\\s]*"Field Value
reg33_1
InternetURL
public const string reg33_1 = "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"Field Value
reg34
手机号码
public const string reg34 = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$"Field Value
reg35
电话号码(“XXX-XXXXXXX”、”XXXX-XXXXXXXX”、”XXX-XXXXXXX”、”XXX-XXXXXXXX”、”XXXXXXX”和”XXXXXXXX)
public const string reg35 = "^($$\\d{3,4}-)|\\d{3.4}-)?\\d{7,8}$"Field Value
reg36
国内电话号码(0511-4405222、021-87888822)
public const string reg36 = "\\d{3}-\\d{8}|\\d{4}-\\d{7}"Field Value
reg37
身份证号(15位、18位数字)
public const string reg37 = "^\\d{15}|\\d{18}$"Field Value
reg38
短身份证号码(数字、字母x结尾)
public const string reg38 = "^([0-9]){7,18}(x|X)?$"Field Value
reg38_1
短身份证号码(数字、字母x结尾)
public const string reg38_1 = "^\\d{8,18}|[0-9x]{8,18}|[0-9X]{8,18}?$"Field Value
reg39
帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)
public const string reg39 = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$"Field Value
reg4
零和非零开头的数字
public const string reg4 = "^(0|[1-9][0-9]*)$"Field Value
reg40
密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线)
public const string reg40 = "^[a-zA-Z]\\w{5,17}$"Field Value
reg41
强密码(必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间)
public const string reg41 = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$"Field Value
reg42
日期格式
public const string reg42 = "^\\d{4}-\\d{1,2}-\\d{1,2}"Field Value
reg43
一年的12个月(01~09和1~12)
public const string reg43 = "^(0?[1-9]|1[0-2])$"Field Value
reg44
一个月的31天(01~09和1~31)
public const string reg44 = "^((0?[1-9])|((1|2)[0-9])|30|31)$"Field Value
reg45
钱的输入格式: 有四种钱的表示形式我们可以接受:”10000.00″ 和 “10,000.00”, 和没有 “分” 的 “10000” 和 “10,000”
public const string reg45 = "^[1-9][0-9]*$"Field Value
reg46
这表示任意一个不以0开头的数字,但是,这也意味着一个字符”0″不通过,所以我们采用下面的形式
public const string reg46 = "^(0|[1-9][0-9]*)$"Field Value
reg47
一个0或者一个不以0开头的数字.我们还可以允许开头有一个负号
public const string reg47 = "^(0|-?[1-9][0-9]*)$"Field Value
reg48
这表示一个0或者一个可能为负的开头不为0的数字.让用户以0开头好了.把负号的也去掉,因为钱总不能是负的吧.下面我们要加的是说明可能的小数部分
public const string reg48 = "^[0-9]+(.[0-9]+)?$"Field Value
reg49
必须说明的是,小数点后面至少应该有1位数,所以”10.”是不通过的,但是 “10” 和 “10.2” 是通过的
public const string reg49 = "^[0-9]+(.[0-9]{2})?$"Field Value
reg5
非零开头的最多带两位小数的数字
public const string reg5 = "^([1-9][0-9]*)+(.[0-9]{1,2})?$"Field Value
reg50
这样我们规定小数点后面必须有两位,如果你认为太苛刻了,可以这样
public const string reg50 = "^[0-9]+(.[0-9]{1,2})?$"Field Value
reg51
这样就允许用户只写一位小数。下面我们该考虑数字中的逗号了,我们可以这样
public const string reg51 = "^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$"Field Value
reg52
1到3个数字,后面跟着任意个 逗号+3个数字,逗号成为可选,而不是必须
public const string reg52 = "^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$"Field Value
reg53
备注:这就是最终结果了,别忘了”+”可以用”*”替代。如果你觉得空字符串也可以接受的话(奇怪,为什么?)最后,别忘了在用函数时去掉去掉那个反斜杠,一般的错误都在这里 xml文件
public const string reg53 = "^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\\\.[x|X][m|M][l|L]$"Field Value
reg54
中文字符的正则表达式
public const string reg54 = "[\\u4e00-\\u9fa5]"Field Value
reg55
双字节字符 (包括汉字在内,可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1))
public const string reg55 = "[^\\x00-\\xff]"Field Value
reg56
空白行的正则表达式,可用来删除空白行
public const string reg56 = "\\n\\s*\\r"Field Value
reg57
HTML标记的正则表达式
public const string reg57 = "<(\\S*?)[^>]*>.*?</\\1>|<.*? />"Field Value
reg58
首尾空白字符的正则表达式
public const string reg58 = "^\\s*|\\s*$)"Field Value
reg58_1
首尾空白字符的正则表达式
public const string reg58_1 = "(^\\s*)|(\\s*$"Field Value
reg59
腾讯QQ号
public const string reg59 = "[1-9][0-9]{4,}"Field Value
reg6
带1-2位小数的正数或负数
public const string reg6 = "^(\\-)?\\d+(\\.\\d{1,2})?$"Field Value
reg60
中国邮政编码
public const string reg60 = "[1-9]\\d{5}(?!\\d)"Field Value
reg61
IP地址
public const string reg61 = "\\d+\\.\\d+\\.\\d+\\.\\d+"Field Value
reg62
IP地址
public const string reg62 = "((?:(?:25[0-5]|2[0-4]\\\\d|[01]?\\\\d?\\\\d)\\\\.){3}(?:25[0-5]|2[0-4]\\\\d|[01]?\\\\d?\\\\d))"Field Value
reg63
提取[]的值
public const string reg63 = "(?is)(?<=\\[)(.*)(?=\\])"Field Value
reg64
提取()的值
public const string reg64 = "(?is)(?<=\\()(.*)(?=\\))"Field Value
reg65
提取{}的值
public const string reg65 = "(?is)(?<=\\{)(.*)(?=\\})"Field Value
reg7
正数、负数、和小数
public const string reg7 = "^(\\-|\\+)?\\d+(\\.\\d+)?$"Field Value
reg8
有两位小数的正实数
public const string reg8 = "^[0-9]+(.[0-9]{2})?$"Field Value
reg9
有1~3位小数的正实数
public const string reg9 = "^[0-9]+(.[0-9]{1,3})?$"Field Value
Methods
IsChineseCharacter(string)
验证只包含汉字
public static bool IsChineseCharacter(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsDateTime(string)
验证日期
public static bool IsDateTime(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsDecimal(string)
验证小数
public static bool IsDecimal(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsEmail(string)
验证电子邮箱
[@字符前可以包含字母、数字、下划线和点号;@字符后可以包含字母、数字、下划线和点号;@字符后至少包含一个点号且点号不能是最后一个字符;最后一个点号后只能是字母或数字]
public static bool IsEmail(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsEnglishCharacter(string)
验证只包含英文字母
public static bool IsEnglishCharacter(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsIDCard(string)
验证身份证号(不区分一二代身份证号)
public static bool IsIDCard(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsIDCard15(string)
验证一代身份证号(15位数)
[长度为15位的数字;匹配对应省份地址;生日能正确匹配]
public static bool IsIDCard15(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsIDCard18(string)
验证二代身份证号(18位数,GB11643-1999标准)
[长度为18位;前17位为数字,最后一位(校验码)可以为大小写x;匹配对应省份地址;生日能正确匹配;校验码能正确匹配]
public static bool IsIDCard18(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsIPv4(string)
验证IPv4地址
[第一位和最后一位数字不能是0或255;允许用0补位]
public static bool IsIPv4(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsIPv6(string)
验证IPv6地址
[可用于匹配任何一个合法的IPv6地址]
public static bool IsIPv6(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsInteger(string)
验证整数
public static bool IsInteger(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsIntegerAndEnglishCharacter(string)
验证只包含数字和英文字母
public static bool IsIntegerAndEnglishCharacter(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsIntegerLength(string, int, int)
验证数字长度范围(数字前端的0计长度)
[若要验证固定长度,可传入相同的两个长度数值]
public static bool IsIntegerLength(string input, int lengthBegin, int lengthEnd)Parameters
input string
待验证的字符串
lengthBegin int
长度范围起始值(含)
lengthEnd int
长度范围结束值(含)
Returns
是否匹配
IsIntegerNotNagtive(string)
验证非负整数
public static bool IsIntegerNotNagtive(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsIntegerPositive(string)
验证正整数
public static bool IsIntegerPositive(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsLatitude(string)
验证纬度
public static bool IsLatitude(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsLongitude(string)
验证经度
public static bool IsLongitude(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsMatch(string, string)
验证字符串是否匹配正则表达式描述的规则
public static bool IsMatch(string inputStr, string patternStr)Parameters
inputStr string
待验证的字符串
patternStr string
正则表达式字符串
Returns
是否匹配
IsMatch(string, string, bool)
验证字符串是否匹配正则表达式描述的规则
public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase = true)Parameters
inputStr string
待验证的字符串
patternStr string
正则表达式字符串
ifIgnoreCase bool
匹配时是否不区分大小写
Returns
是否匹配
IsMatch(string, bool, string)
验证字符串是否匹配正则表达式描述的规则
public static bool IsMatch(string inputStr, bool ifValidateWhiteSpace, string patternStr)Parameters
inputStr string
待验证的字符串
ifValidateWhiteSpace bool
是否验证空白字符串
patternStr string
正则表达式字符串
Returns
是否匹配
IsMatch(string, string, bool, bool)
验证字符串是否匹配正则表达式描述的规则
public static bool IsMatch(string inputStr, string patternStr, bool ifIgnoreCase, bool ifValidateWhiteSpace)Parameters
inputStr string
待验证的字符串
patternStr string
正则表达式字符串
ifIgnoreCase bool
匹配时是否不区分大小写
ifValidateWhiteSpace bool
是否验证空白字符串
Returns
是否匹配
IsMobilePhoneNumber(string)
验证手机号码
[可匹配"(+86)013325656352",括号可以省略,+号可以省略,(+86)可以省略,11位手机号前的0可以省略;11位手机号第二位数可以是3、4、5、8中的任意一个]
public static bool IsMobilePhoneNumber(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsNumber(string)
验证数字(double类型)
[可以包含负号和小数点]
public static bool IsNumber(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsPhoneNumber(string)
验证电话号码(可以是固定电话号码或手机号码)
[固定电话:[3位或4位区号;区号可以用小括号括起来;区号可以省略;区号与本地号间可以用减号或空格隔开;可以有3位数的分机号,分机号前要加减号]]
[手机号码:[可匹配"(+86)013325656352",括号可以省略,+号可以省略,(+86)可以省略,手机号前的0可以省略;手机号第二位数可以是3、4、5、8中的任意一个]]
public static bool IsPhoneNumber(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsStringByteLength(string, int, int)
验证字符串字节数长度范围
[若要验证固定长度,可传入相同的两个长度数值;每个汉字为两个字节长度]
public static bool IsStringByteLength(string input, int lengthBegin, int lengthEnd)Parameters
input string
待验证的字符串
lengthBegin int
长度范围起始值(含)
lengthEnd int
长度范围结束值(含)
Returns
IsStringInclude(string, bool, bool, bool)
验证字符串包含内容
public static bool IsStringInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter)Parameters
input string
待验证的字符串
withEnglishCharacter bool
是否包含英文字母
withNumber bool
是否包含数字
withChineseCharacter bool
是否包含汉字
Returns
是否匹配
IsStringLength(string, int, int)
验证字符串长度范围
[若要验证固定长度,可传入相同的两个长度数值]
public static bool IsStringLength(string input, int lengthBegin, int lengthEnd)Parameters
input string
待验证的字符串
lengthBegin int
长度范围起始值(含)
lengthEnd int
长度范围结束值(含)
Returns
是否匹配
IsStringLengthByInclude(string, bool, bool, bool, int, int)
验证字符串长度范围
[若要验证固定长度,可传入相同的两个长度数值]
public static bool IsStringLengthByInclude(string input, bool withEnglishCharacter, bool withNumber, bool withChineseCharacter, int lengthBegin, int lengthEnd)Parameters
input string
待验证的字符串
withEnglishCharacter bool
是否包含英文字母
withNumber bool
是否包含数字
withChineseCharacter bool
是否包含汉字
lengthBegin int
长度范围起始值(含)
lengthEnd int
长度范围结束值(含)
Returns
是否匹配
IsStringLengthOnlyNumberAndEnglishCharacter(string, int, int)
验证字符串长度范围(字符串内只包含数字和/或英文字母)
[若要验证固定长度,可传入相同的两个长度数值]
public static bool IsStringLengthOnlyNumberAndEnglishCharacter(string input, int lengthBegin, int lengthEnd)Parameters
input string
待验证的字符串
lengthBegin int
长度范围起始值(含)
lengthEnd int
长度范围结束值(含)
Returns
是否匹配
IsTelePhoneNumber(string)
验证固定电话号码
[3位或4位区号;区号可以用小括号括起来;区号可以省略;区号与本地号间可以用减号或空格隔开;可以有3位数的分机号,分机号前要加减号]
public static bool IsTelePhoneNumber(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsURL(string)
验证网址(可以匹配IPv4地址但没对IPv4地址进行格式验证;IPv6暂时没做匹配)
[允许省略"😕/";可以添加端口号;允许层级;允许传参;域名中至少一个点号且此点号前要有内容]
public static bool IsURL(string input)Parameters
input string
待验证的字符串
Returns
是否匹配
IsZipCode(string)
验证邮政编码
public static bool IsZipCode(string input)Parameters
input string
待验证的字符串
Returns
是否匹配