jquery 常用校驗總結

常用校驗


var mString = {
    /**
    *字符串去空格
    */
    trim : function(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },

    /**
     * 判斷是否是空字符串或null
     * 
     * @param {Object}
     *            str
     */
    isBlank : function(str) {
        if (!str || this.trim(str) == "") {
            return true;
        } else {
            return false;
        }
    },

    /**
     * 判斷是否是空字符串或null
     * 
     * @param {Object}
     *            str
     */
    isNotBlank : function(str) {
        if (!str || this.trim(str) == "") {
            return false;
        } else {
            return true;
        }
    },

    /**
     * 日期比較
     * @param startTime - 開始時間 2016-09-09
     * @param endTime - 結束時間  2016-09-10
     * @returns {Boolean}
     */
    compareTime : function(startTime, endTime){
        // 開始時間
        var start=new Date(startTime.replace("-", "/").replace("-", "/"));
        // 結束時間
        var end=new Date(endTime.replace("-", "/").replace("-", "/"));

        if(end < start){
            return false;
        }
        return true;
    },

    //創建uuid
    getUuid : function(){
        return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random() * 16 | 0,
                v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    },

    /**
     * 格式化字符串
     * @param date 日期字符串,new Date()
     * @param format 格式化格式如 yyyy-MM-dd 或者 yyyy-MM-dd HH:mm:ss
     * @returns
     */
    formatDate : function(date, format){
        if (!date) return "";
        if (!format) format = "yyyy-MM-dd";   
        switch(typeof date) {   
            case "string":
                if(date.length==8){
                    date = date.substring(0,4)+"/"+date.substring(4,6)+"/"+date.substring(6,8);
                }else if(date.length==14){
                    date = date.substring(0,4)+"/"+date.substring(4,6)+"/"+date.substring(6,8)+" "+date.substring(8,10)+":"+date.substring(10,12)+":"+date.substring(12,14);
                }
                date = new Date(date);   
                break;   
            case "number":   
                date = new Date(date);   
                break;   
        }    
        if (!date instanceof Date) return;   
        var dict = {   
            "yyyy": date.getFullYear(),   
            "M": date.getMonth() + 1,   
            "d": date.getDate(),   
            "H": date.getHours(),   
            "m": date.getMinutes(),   
            "s": date.getSeconds(),   
            "MM": ("" + (date.getMonth() + 101)).substr(1),   
            "dd": ("" + (date.getDate() + 100)).substr(1),   
            "HH": ("" + (date.getHours() + 100)).substr(1),   
            "mm": ("" + (date.getMinutes() + 100)).substr(1),   
            "ss": ("" + (date.getSeconds() + 100)).substr(1)   
        };       
        return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function() {   
            return dict[arguments[0]];   
        });
    }
};


/**
 * 驗證身份證準確性
 * @param num
 * @returns 準確 返回true
 */
function isIdCardError(num){
    if ((/^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(num))){
        return true;
    }else{
        return false;
    }
}

/**
 * 驗證電話號碼
 * @param num
 * @returns 是 返回true
 */
function isPhone(num){
    if ((/^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(num))){
        return true;
    }else{
        return false;
    }
}

/**
 * 驗證郵政編碼
 * @param num
 * @returns 是 返回true
 */
function isZip(num){
    if ((/^[1-9]\d{5}$/i.test(num))){
        return true;
    }else{
        return false;
    }
}

/**
 * 驗證手機號碼
 * @param num
 * @returns 是 返回true
 */
function isMobile(num){
    if ((/^(13|15|18)\d{9}$/i.test(num))){
        return true;
    }else{
        return false;
    }
}

/**
 * 驗證輸入是否是中文
 * @param value
 * @returns 是:true 否:false
 */
function isChina(value){
    return /^[\Α-\¥]+$/i.test(value); 
}

/**
 * 驗證是否是英文
 * @param value
 * @returns 是:true 否:false
 */
function isEnglish(value){
    return /^[A-Za-z]+$/i.test(value);
}

/**
 * 驗證是否包含空格和非法字符 
 * @param data
 * @returns 含非法字符或者空格返回false,否則true
 */
function isIllegal(value){
    return /.+/i.test(value); 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章