正則表達式(收集)


只能輸入數字:"^[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-5678

SSN: ###-##-#### for example: 123-45-6789

EIN: ##-####### for example: 90-0355555

function 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="";
        
        }
    
    }

 同類: http://www.cnblogs.com/coacaio/archive/2011/12/24/2300681.html                                                                                                                                                                                                                    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM