JavaScript數據驗證腳本

function ByteString() {   
    //判斷是不是爲正整數   
    this.checkThanZero = function(str) {   
        if (/^[0-9]*[1-9][0-9]*$/.test(str))   
            return true;   
        else  
            return false;   
    };   
    // 對字符串進行trim   
    this.trim = function(str) {   
        return str.replace(/(^\s*)|(\s*$)/g, "");   
    };   
    /**  
     * 判斷字符狀態,如果字符是數字返回1,大寫字母返回2,小寫字母返回3,特殊字符返回4  
     */  
    this.charMode = function(iN) {   
        if (iN >= 48 && iN <= 57) // 數字   
            return 1;   
        if (iN >= 65 && iN <= 90) // 大寫字母   
            return 2;   
        if (iN >= 97 && iN <= 122) // 小寫   
            return 3;   
        else  
            return 4; // 特殊字符   
    };   
    /**  
     * 計算字符串的字符數,注:一箇中文爲兩個字符  
     */  
    this.getStrLen = function(Obj) {   
        var nCNLenth = 0;   
        var nLenth = Obj.length;   
        for ( var i = 0; i < nLenth; i++) {   
            if (Obj.charCodeAt(i) > 255) {   
                nCNLenth += 2;   
            } else {   
                nCNLenth++;   
            }   
        }   
        return nCNLenth;   
    };   
    /**  
     * 判斷是不是爲數字,如果mun,max都爲數字,num <= max,並且num > 0返回TRUE,否則返回FALSE  
     */  
    this.checkNumber = function(num, max) {   
        if (/^[0-9]+$/.test(num) && num <= max && num > 0)   
            return true;   
        else  
            return false;   
    };   
    // 判斷是否爲數字和字母組合的一種或者兩種 如果出現除數字字母外的其餘字符返回false 否則返回true   
    this.checkEnglishAndNumber = function(str) {   
        if (/^[A-Za-z0-9]+$/.test(str))   
            return true;   
        else  
            return false;   
    };   
    /**  
     * 是否爲聯通G網用戶 是返回 true 否則返回 false  
     */  
    this.isGNet = function(str) {   
        if (str.length != 11)   
            return false;   
        if (/^13[0-2][0-9]{8}|15[5-6][0-9]{8}|18[6][0-9]{8}$/.test(str))   
            return true;   
        else  
            return false;   
    };   
    // 判斷是字符串是不是整數,是返回true 否則返回false   
    this.checkInteger = function(str) {   
        if (/^-?\d+$/.test(str))   
            return true;   
        else  
            return false;   
    };   
    /**  
     * 判斷是否爲1901(含1901)到現在的某一年,是返回TRUE,否則返回FALSE  
     */  
    this.checkYear = function(year) {   
        if (year == "")   
            return false;   
        if (!this.checkNumber(year, new Date().getFullYear())   
                || parseInt(year) < 1901)   
            return false;   
        return true;   
    };   
    /**  
     * 判斷是否爲1-12的某一月,是返回TRUE,否則返回FALSE  
     */  
    this.checkMonth = function(month) {   
        if (month == "")   
            return false;   
        if (!this.checkNumber(month, 12))   
            return false;   
        return true;   
    };   
    /**  
     * 檢查從1901 到今年的某天是否存在(如果是今年的某天不一定已經過去) 存在返回FALSE 不存在返回TRUE  
     */  
    this.checkDay = function(year, month, day) {   
        if (!this.checkYear(year)) {   
            return false;   
        }   
        if (!this.checkMonth(month)) {   
            return false;   
        }   
        var maxday = 31;   
        switch (parseInt(month)) {   
        case 1:   
            maxday = 31   
            break  
        case 2:   
            if (isLeapYear(year))   
                maxday = 29   
            else  
                maxday = 28   
            break  
        case 3:   
            maxday = 31   
            break  
        case 4:   
            maxday = 30   
            break  
        case 5:   
            maxday = 31   
            break  
        case 6:   
            maxday = 30   
            break  
        case 7:   
            maxday = 31   
            break  
        case 8:   
            maxday = 31   
            break  
        case 9:   
            maxday = 30   
            break  
        case 10:   
            maxday = 31   
            break  
        case 11:   
            maxday = 30   
            break  
        case 12:   
            maxday = 31   
            break  
        default:   
            maxday = 31;   
        }   
        if (!this.checkNumber(day, maxday))   
            return false;   
        return true;   
    };   
    /**  
     * 判斷是否爲手機號碼  
     */  
    this.checkMobile = function(mobile) {   
        if (mobile == "")   
            return false;   
        if (/^13\d{9}$/.test(mobile) | /^15\d{9}$/.test(mobile)   
                | /^18\d{9}$/.test(mobile))   
            return true;   
        return false;   
    };   
    /**  
     * 判斷是否爲G網手機,即聯通非CDMA手機號碼  
     */  
    this.isGNet = function(mobile) {   
        if (mobile == "")   
            return false;   
        if (/^13[0-2][0-9]{8}|15[5-6][0-9]{8}|18[6][0-9]{8}$/.test(mobile))   
            return true;   
        return false;   
    };   
    /**  
     * 判斷是否爲C網手機,CDMA手機號碼  
     */  
    this.isCNet = function(mobile) {   
        if (mobile == "")   
            return false;   
        if (/^13[3][0-9]{8}|15[3][0-9]{8}|18[9][0-9]{8}$/.test(mobile))   
            return true;   
        return false;   
    };   
    /**  
     * 判斷是否爲移動號碼  
     */  
    this.isMobileNet = function(mobile) {   
        if (mobile == "")   
            return false;   
        if (/^13[4-9][0-9]{8}|15[01289][0-9]{8}$/.test(mobile))   
            return true;   
        return false;   
    };   
    // 判斷閏年   
    this.isLeapYear = function(year) {   
        return (0 == year % 4 && ((year % 100 != 0) || (year % 400 == 0)));   
    };   
    //使Id爲objId的下拉框中option值爲optionVal的項選中,如果沒有這一項,不對select進行任何操作   
    this.makeOptionSelected=function(objId,optionVal)   
    {   
        var sel=document.getElementById(objId);   
        for(var i=0;i<sel.length;i++)      
        {   
            if(sel.options[i].value==optionVal)   
            {   
                sel.options[i].selected="selected";   
                break ;   
            }   
        }   
    };   
}  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章