2012-12-12 15:22 by 露珠的微笑,900阅读,0评论,收藏,编辑
只能输入数字:@H_502_8@"@H_502_8@^[0-9]*$@H_502_8@"。 只能输入n位的数字:@H_502_8@^\d{n}$@H_502_8@"。 只能输入至少n位的数字:@H_502_8@^\d{n,}$@H_502_8@"。 只能输入m~n位的数字:。@H_502_8@^\d{m,n}$@H_502_8@" 只能输入零和非零开头的数字:@H_502_8@^(0|[1-9][0-9]*)$@H_502_8@"。 只能输入有两位小数的正实数:@H_502_8@^[0-9]+(.[0-9]{2})?$@H_502_8@"。 只能输入有1~3位小数的正实数:@H_502_8@^[0-9]+(.[0-9]{1,3})?$@H_502_8@"。 只能输入非零的正整数:@H_502_8@^\+?[1-9][0-9]*$@H_502_8@"。 只能输入非零的负整数:@H_502_8@^\-[1-9][]0-9@H_502_8@"*$。 只能输入长度为3的字符:@H_502_8@^.{3}$@H_502_8@"。 只能输入由26个英文字母组成的字符串:@H_502_8@^[A-Za-z]+$@H_502_8@"。 只能输入由26个大写英文字母组成的字符串:@H_502_8@^[A-Z]+$@H_502_8@"。 只能输入由26个小写英文字母组成的字符串:@H_502_8@^[a-z]+$@H_502_8@"。 只能输入由数字和26个英文字母组成的字符串:@H_502_8@^[A-Za-z0-9]+$@H_502_8@"。 只能输入由数字、26个英文字母或者下划线组成的字符串:@H_502_8@^\w+$@H_502_8@"。 验证用户密码:@H_502_8@^[a-zA-Z]\w{5,17}$@H_502_8@"正确格式为:以字母开头,长度在6~18之间,只能包含字符、数字和下划线。 验证是否含有^%&@H_502_8@'@H_502_8@,;=?$\"等字符:"[^%&@H_502_8@',;=?$\x22]+@H_502_8@。 只能输入汉字:@H_502_8@^[\u4e00-\u9fa5]{0,0); line-height:1.5!important">" //CN:是否为空或者全部都是空格 String.prototype.isNull = function() { return this.regex(/^\s*$/); } EN: CN:是否为邮箱 String.prototype.isEmail = function() { this.regex(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/); } CN:是否只由英文字母和数字和下划线组成 String.prototype.isNumberOr_Letter = function () { this.regex(/^[0-9a-zA-Z\_]+$/); } CN:是否有包含特殊字符 String.prototype.isScript = function () { this.regex(/(<[\/]?script.*>)/i); } 是否为空或者全部都是空格 String.prototype.isNull = function() { CN:只能输入数字字母下划线,输入的长度自定义 String.prototype.isscript = function(minlen,maxlen) { return this.regex(/^[a-zA-Z0-9_]{minlen,maxlen}$/); } String.prototype.isscript = function() { true; } this.regex(/^[a-zA-Z0-9_]+$/); 长度是否在指定的范围 String.prototype.isLen = function(minlen,maxlen) { this.length >= minlen && this.length <= maxlen ? true : false; } 只能输入由数字、26个英文字母或者下划线组成的字符串 String.prototype.isscript1 = function() { this.regex(/^\w+$/); } 以字母开头,长度在6~18之间,只能包含字符、数字和下划线。(通常用在密码:) String.prototype.ispwd = function() { this.regex(/^[a-zA-Z]\w{5,17}$/); } 只能输入数字 String.prototype.isnum = function() { 0-9]*$/); } 只能输入8位的数字 String.prototype.isonlynum = function() { this.regex(/^\d{8}$/); } 是否为日期格式(yyyy/mm/dd) String.prototype.isDate1 = function() { 1,128); line-height:1.5!important">4}(-|\/|\.|年)(0?[1-9]|1[2])(-|\/|\.|月)(9]|[12]\d|3[01])(日)?$/); } 是否为日期格式(mm/dd/yyyy) String.prototype.isDate = function() { this.regex(/^(01])(-|\/|\.|日)(\d{4})?$/); } //CN:是否有包含特殊字符 String.prototype.isScript = function () { return this.regex(/(<[\/]?script.*>)/i); }
============================================================ Created By: LiYing Zheng,EM_Shop_20120709,7/09/2012 ============================================================ using System; using System.Text.RegularExpressions; /// <summary> ///Valid 的摘要说明 </summary> public static class Valid { #region 空的各种验证 为空返回turn(空格键也行,null也行) bool isEmpty(this string str) { return str == null ? true : Regex.IsMatch(str,0); line-height:1.5!important">@"@H_502_8@^\s*$@H_502_8@"); } bool isNotEmpty(return !isEmpty(str); } 为空返回turn(空格键) bool isNull(return Regex.IsMatch(str,0); line-height:1.5!important">^ \\s*$#endregion #region 长度的各种验证 输入6~16位的数字返回turn(6:可以改动) bool isLeast(^\d{6,16}$#endregion #region 汉字、字母、数字、下划的各种验证 为数字格式返回turn bool isNumber(^\d+$@H_502_8@"); } 为数字和26个英文字母组成的字符串 返回turn bool isNumberOrLetter(^[0-9a-zA-Z]+$@H_502_8@为数字、26个英文字母或者下划线组成的字符串 返回turn bool isNumberOrLetterOrUnderscore(以字母开头,长度在6~18之间,只能包含字符、数字和下划线。返回turn bool isNumberOrLetterOrUnderscore1(^[a-zA-Z]\w{3,0); line-height:1.5!important">长度在6~18之间,只能包含字符、数字和下划线。返回turn bool isNumberOrLetterOrUnderscore2(^\w{3,0); line-height:1.5!important">为汉字、字母、数字 返回turn bool isChinaOrLett(^[\w\u4e00-\u9fa5]+$@H_502_8@为汉字、字母 返回turn bool isChinaOrNumbOrLett(^[a-zA-Z\u4e00-\u9fa5]+$@H_502_8@以汉字、字母、数字、下划线组成(不能以数字开头) 返回turn bool isNoNumfirst(^[a-zA-Z_\u4e00-\u9fa5][0-9a-zA-Z_\u4e00-\u9fa5]+$@H_502_8@以!、@、#、$、%、&、*、汉字、字母、数字、下划线组成 返回turn bool isScpritOrOther(^[\\%,\\#,\\*,\\$,\\@,\\!,\\&\0-9a-zA-Z_\u4e00-\u9fa5]{1,255); line-height:1.5!important">#region 特殊字符的各种验证 为‘< >’返回turn bool isSpChar((<|>)@H_502_8@ 验证是否含有特殊等字符 返回turn 你自己需要什么特殊符号加个‘,\\#’就可以了 @"^[,\\`,\\~,\\',\\"",\\=,\\<,\\>,\\#]+$" bool isScript(^[,\\&\w]+$@H_502_8@是否为身份证号 bool isPID((^\d{15}$)|(^\d{17}([0-9]|X|x)$)@H_502_8@是否为邮箱格式 bool isEmail(^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$@H_502_8@是否为电话格式 正确格式为:"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXX- XXXXXXX"、"XXXX- XXXXXXXX" bool isPhone(^(0\d{2,3}-)?\d{7,8}(-\d{1,4})?$@H_502_8@是否为手机格式 bool isMobile(^1(3|5|8)\d{9}$@H_502_8@是否为传真格式 bool isFax(^86\-(\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,0); line-height:1.5!important">是否为图片格式‘.jpg’ bool isImage(return Regex.IsMatch(str.ToLower(),0); line-height:1.5!important">.(jpg|gif|png|jpeg)$@H_502_8@是否为钱的格式 bool isMoney(^\d{1,8}(,\d{3})*(\.\d{1,2})?$@H_502_8@是否为QQ号码格式 bool isQQ(^[1-9]\d{4,11}$@H_502_8@是否为日期格式 ****-**-** bool isDate(502_8@是否为域名格式 3d_.dfss.cn.com bool isDomain(^([\w-]+\.)+[\w-]+$@H_502_8@是否为网址格式(带 http: | https: | ftp: 这个开头) bool isWebUrl(string str) { ^(http|https|ftp):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$@H_502_8@是否为网站地址格式(不带 http:bool isUrl(^([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$@H_502_8@将用户输入到控件中的非法字符转译成合法字符 string parseString(string str) { if (str.IndexOf(@H_502_8@\'@H_502_8@")!=-1) { str = str.Replace(@H_502_8@",0); line-height:1.5!important">'@H_502_8@"); } \"@H_502_8@“@H_502_8@<1) { str =str .Replace(@H_502_8@<@H_502_8@>@H_502_8@") != -@H_502_8@>@H_502_8@&@H_502_8@&return str; } }Phone,: (###)###-#### for example: (312)234-5678 SSN: ###-##-#### for example: 123-45-6789 EIN: ##-####### 90-0355555 function MaskPhone(objNpt,callermsg){ var n=objNpt.value.replace(/[^\d]+/g,0); line-height:1.5!important">''); replace all non digits if (n.length!=10) { alert(@H_502_8@The @H_502_8@" + callermsg + @H_502_8@ number you enter is not valid! Please input 10 digits only. No text in this field.@H_502_8@"); objNpt.value=@H_502_8@""; } else objNpt.value=n.replace(/(\d\d\d)(\d\d\d)(\d)/,0); line-height:1.5!important">($1)$2-$3@H_502_8@'); format the number } function MaskSSN(objNpt){ 9) { alert(@H_502_8@The social Security Number you enter is not valid! Please input 9 digits. else objNpt.value=n.replace(/(\d\d\d)(\d\d)(\d\d\d\d)/,0); line-height:1.5!important">$1-$2-$3@H_502_8@ format the number } function MaskEIN(objNpt){ The EIN Number you enter is not valid! Please input 9 digits. else objNpt.value=n.replace(/(\d\d)(\d)/,0); line-height:1.5!important">$1-$2@H_502_8@ format the number } function MaskZIP(objNpt){ if (n.length==5) { objNpt.value=n; } else 9){ objNpt.value=n.replace(/(\d\d\d\d\d)(\d)/,0); line-height:1.5!important">');} format the number else { alert(@H_502_8@The Zip code you enter is not valid! Please either enter 5 digit zip or 9 digit zip+extension. @H_502_8@"";} } function MaskEmail(objNpt) { var RegEmail=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; if(!RegEmail.test(objNpt.value)) { alert(@H_502_8@The Email number you enter is not valid!@H_502_8@"); objNpt.value=@H_502_8@""; } }