正则表达式
只能输入数字:"^[0-9]*$"。
只能输入n位的数字:"^d{n}$"。 只能输入至少n位的数字:"^d{n,}$"。 只能输入m~n位的数字:。"^d{m,n}$" 只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。 只能输入有两位小数的正实数:"^[0-9]+(.[0-9]{2})?$"。 只能输入有1~3位小数的正实数:"^[0-9]+(.[0-9]{1,3})?$"。 只能输入非零的正整数:"^+?[1-9][0-9]*$"。 只能输入非零的负整数:"^-[1-9][]0-9"*$。 只能输入长度为3的字符:"^.{3}$"。 只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。 只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。 只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。 只能输入由数字和26个英文字母组成的字符串:"^[A-Za-z0-9]+$"。 只能输入由数字、26个英文字母或者下划线组成的字符串:"^w+$"。 验证用户密码:"^[a-zA-Z]w{5,17}$"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。 验证是否含有^%&',;=?$"等字符:"[^%&',;=?$x22]+"。 只能输入汉字:"^[u4e00-u9fa5]{0,}$" //CN:是否为空或者全部都是空格 String.prototype.isNull = function() { return this.regex(/^s*$/); } //EN: //CN:是否为邮箱 String.prototype.isEmail = function() { return this.regex(/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/); } //EN: //CN:是否只由英文字母和数字和下划线组成 String.prototype.isNumberOr_Letter = function () { return this.regex(/^[0-9a-zA-Z_]+$/); } //EN: //CN:是否有包含特殊字符 String.prototype.isScript = function () { return this.regex(/(<[/]?script.*>)/i); } //是否为空或者全部都是空格 String.prototype.isNull = function() { return this.regex(/^s*$/); } //EN: //CN:只能输入数字字母下划线,输入的长度自定义 //String.prototype.isscript = function(minlen,maxlen) { return this.regex(/^[a-zA-Z0-9_]{minlen,maxlen}$/); } String.prototype.isscript = function() { return true; } //this.regex(/^[a-zA-Z0-9_]+$/); //长度是否在指定的范围 String.prototype.isLen = function(minlen,maxlen) { return this.length >= minlen && this.length <= maxlen ? true : false; } //只能输入由数字、26个英文字母或者下划线组成的字符串 String.prototype.isscript1 = function() { return this.regex(/^w+$/); } //以字母开头,长度在6~18之间,只能包含字符、数字和下划线。(通常用在密码:) String.prototype.ispwd = function() { return this.regex(/^[a-zA-Z]w{5,17}$/); } //只能输入数字 String.prototype.isnum = function() { return this.regex(/^[0-9]*$/); } //只能输入8位的数字 String.prototype.isonlynum = function() { return this.regex(/^d{8}$/); } //是否为日期格式(yyyy/mm/dd) String.prototype.isDate1 = function() { return this.regex(/^d{1,4}(-|/|.|年)(0?[1-9]|1[0-2])(-|/|.|月)(0?[1-9]|[12]d|3[01])(日)?$/); } //是否为日期格式(mm/dd/yyyy) String.prototype.isDate = function() { return this.regex(/^(0?[1-9]|1[0-2])(-|/|.|月)(0?[1-9]|[12]d|3[01])(-|/|.|日)(d{4})?$/); } //CN:是否有包含特殊字符 String.prototype.isScript = function () { return this.regex(/(<[/]?script.*>)/i); }
/// <summary> ///Valid 的摘要说明 /// </summary> public static class Valid { #region 空的各种验证 //为空返回turn(空格键也行,null也行) public static bool isEmpty(this string str) { return str == null ? true : Regex.IsMatch(str,@"^s*$"); } public static bool isNotEmpty(this string str) { return !isEmpty(str); } //为空返回turn(空格键) public static bool isNull(this string str) { return Regex.IsMatch(str,"^ s*$"); } #endregion #region 长度的各种验证 //输入6~16位的数字返回turn(6:可以改动) public static bool isLeast(this string str) { return Regex.IsMatch(str,@"^d{6,16}$"); } #endregion #region 汉字、字母、数字、下划的各种验证 //为数字格式返回turn public static bool isNumber(this string str) { return Regex.IsMatch(str,@"^d+$"); } //为数字和26个英文字母组成的字符串 返回turn public static bool isNumberOrLetter(this string str) { return Regex.IsMatch(str,"^[0-9a-zA-Z]+$"); } //为数字、26个英文字母或者下划线组成的字符串 返回turn public static bool isNumberOrLetterOrUnderscore(this string str) { return Regex.IsMatch(str,@"^w+$"); } //以字母开头,长度在6~18之间,只能包含字符、数字和下划线。返回turn public static bool isNumberOrLetterOrUnderscore1(this string str) { return Regex.IsMatch(str,@"^[a-zA-Z]w{3,16}$"); } //长度在6~18之间,只能包含字符、数字和下划线。返回turn public static bool isNumberOrLetterOrUnderscore2(this string str) { return Regex.IsMatch(str,@"^w{3,16}$"); } //为汉字、字母、数字 返回turn public static bool isChinaOrLett(this string str) { return Regex.IsMatch(str,@"^[wu4e00-u9fa5]+$"); } //为汉字、字母 返回turn public static bool isChinaOrNumbOrLett(this string str) { return Regex.IsMatch(str,@"^[a-zA-Zu4e00-u9fa5]+$"); } //以汉字、字母、数字、下划线组成(不能以数字开头) 返回turn public static bool isNoNumfirst(this string str) { return Regex.IsMatch(str,@"^[a-zA-Z_u4e00-u9fa5][0-9a-zA-Z_u4e00-u9fa5]+$"); } //以!、@、#、$、%、&、*、汉字、字母、数字、下划线组成 返回turn public static bool isScpritOrOther(this string str) { return Regex.IsMatch(str,@"^[%,#,*,$,@,!,& |