正则表达式(收集)

来源:互联网 发布:网站如何做谷歌优化 编辑:程序博客网 时间:2024/05/16 23:41

2012-12-12 15:22 by 露珠的微笑, 900 阅读, 0 评论, 收藏, 编辑复制代码

只能输入数字:"^[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); }
复制代码
// ============================================================// 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也行)    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, @"^[\w\u4e00-\u9fa5]+$");    }    //为汉字、字母 返回turn    public static bool isChinaOrNumbOrLett(this string str)    {        return Regex.IsMatch(str, @"^[a-zA-Z\u4e00-\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, @"^[\\%,\\#,\\*,\\$,\\@,\\!,\\&\0-9a-zA-Z_\u4e00-\u9fa5]{1,16}$");    }    #endregion    #region 特殊字符的各种验证    //为‘< >’返回turn    public static bool isSpChar(this string str)    {        return Regex.IsMatch(str, "(<|>)");    }    // 验证是否含有特殊等字符  返回turn    //你自己需要什么特殊符号加个‘,\\#’就可以了    //@"^[ ,\\`,\\~,\\',\\"",\\=,\\<,\\>,\\#]+$"    public static bool isScript(this string str)    {        return Regex.IsMatch(str, @"^[ ,\\`,\\~,\\',\\"",\\=,\\<,\\>,\\&\w]+$");    }    #endregion    //是否为身份证号    public static bool isPID(this string str)    {        return Regex.IsMatch(str, @"(^\d{15}$)|(^\d{17}([0-9]|X|x)$)");    }    //是否为邮箱格式    public static bool isEmail(this string str)    {        return Regex.IsMatch(str, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");    }    //是否为电话格式    //正确格式为:"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXX- XXXXXXX"、"XXXX- XXXXXXXX"    public static bool isPhone(this string str)    {        return Regex.IsMatch(str, @"^(0\d{2,3}-)?\d{7,8}(-\d{1,4})?$");    }    //是否为手机格式     public static bool isMobile(this string str)    {        return Regex.IsMatch(str, @"^1(3|5|8)\d{9}$");    }    //是否为传真格式    public static bool isFax(this string str)    {        return Regex.IsMatch(str, @"^86\-(\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,4})?$");    }    //是否为图片格式‘.jpg’    public static bool isImage(this string str)    {        return Regex.IsMatch(str.ToLower(), ".(jpg|gif|png|jpeg)$");    }    //是否为钱的格式    public static bool isMoney(this string str)    {        return Regex.IsMatch(str, @"^\d{1,8}(,\d{3})*(\.\d{1,2})?$");    }    //是否为QQ号码格式    public static bool isQQ(this string str)    {        return Regex.IsMatch(str, @"^[1-9]\d{4,11}$");    }    //是否为日期格式 ****-**-**    public static bool isDate(this string str)    {        return Regex.IsMatch(str, @"^\d{1,4}(-|\/|\.|年)(0?[1-9]|1[0-2])(-|\/|\.|月)(0?[1-9]|[12]\d|3[01])(日)?$");    }    //是否为域名格式 3d_.dfss.cn.com    public static bool isDomain(this string str)    {        return Regex.IsMatch(str, @"^([\w-]+\.)+[\w-]+$");    }    //是否为网址格式(带 http:// | https:// | ftp:// 这个开头)    public static bool isWebUrl(this string str)    {         return Regex.IsMatch(str, @"^(http|https|ftp):\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$");    }    //是否为网站地址格式(不带 http:// | https:// | ftp:// 这个开头)    public static bool isUrl(this string str)    {        return Regex.IsMatch(str, @"^([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$");    }    //将用户输入到控件中的非法字符转译成合法字符    public static string parseString(this string str)    {                    if (str.IndexOf("\'")!=-1) {                str = str.Replace("\'", "");            }            if (str.IndexOf("\"")!=-1) {                str = str.Replace("\"", "");            }            if (str.IndexOf("<")!=-1) {                str =str .Replace("<", "");            }            if (str.IndexOf(">") != -1) {                str = str.Replace(">", "");            }            if (str.IndexOf("&") != -1) {                str = str.Replace("&", "");            }            return str;                }}
复制代码
复制代码
Phone,: (###)###-####  for example: (312)234-5678SSN: ###-##-#### for example: 123-45-6789EIN: ##-####### for example: 90-0355555function MaskPhone(objNpt, callermsg){    var n=objNpt.value.replace(/[^\d]+/g,'');// replace all non digits    if (n.length!=10) {        alert("The " + callermsg + " number you enter is not valid! Please input 10 digits only.  No text in this field.");                objNpt.value="";        }            else        objNpt.value=n.replace(/(\d\d\d)(\d\d\d)(\d)/,'($1)$2-$3');//  format the number}function MaskSSN(objNpt){    var n=objNpt.value.replace(/[^\d]+/g,'');// replace all non digits    if (n.length!=9) {        alert("The social Security Number you enter is not valid! Please input 9 digits. ");                objNpt.value="";        }            else        objNpt.value=n.replace(/(\d\d\d)(\d\d)(\d\d\d\d)/,'$1-$2-$3');// format the number}    function MaskEIN(objNpt){    var n=objNpt.value.replace(/[^\d]+/g,'');// replace all non digits    if (n.length!=9) {        alert("The EIN Number you enter is not valid! Please input 9 digits. ");                objNpt.value="";        }            else        objNpt.value=n.replace(/(\d\d)(\d)/,'$1-$2');// format the number}function MaskZIP(objNpt){    var n=objNpt.value.replace(/[^\d]+/g,'');// replace all non digits    if (n.length==5) {       objNpt.value=n;        }    else if (n.length==9){        objNpt.value=n.replace(/(\d\d\d\d\d)(\d)/,'$1-$2');}// format the number    else        { alert("The Zip code you enter is not valid! Please either enter 5 digit zip or 9 digit zip+extension. ");                objNpt.value="";}        }function MaskEmail(objNpt){    var RegEmail=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;    if(!RegEmail.test(objNpt.value))    {        alert("The Email  number you enter is not valid!");        objNpt.value="";                }        }

0 0