JS 工具类 读取cookie 用户名验证 邮箱验证 手机号码验证 trim字符串 验证正整数 判断闰年 等

function ByteString() {
    //读取cookie
    //cookieName cookie属性名称
    //如果存在这个cookie返回这个cookie,否则返回"-1"
    this.getCookie = function(cookieName){
        var cookieStr = document.cookie; //取 cookie 字符串
        if (cookieStr == ""){
        return "-1"; //没有取到 cookie 字符串,返回默认值
        }
        //alert(cookieStr);
        //将各个 cookie 分隔开,并存为数组,多个 cookie 之间用分号加空隔隔开
        var cookieValue = cookieStr.split("; ");
        for (var i=0; i<cookieValue.length; i++){
            var cookieAll=cookieValue[i];
            var cookieTempName=cookieAll.substring(0,cookieAll.indexOf('='));
            //alert(cookieTempName);
            if(cookieName==cookieTempName){
                return cookieAll.substring(cookieAll.indexOf('=')+1);
            }
        }
        return "-1";     
    }
    //放入一个到2020年过期的cookie
    //cookieName cookie名称
    //cookieValue cookie值
    //如果客户端cookie不可写 返回FALSE
    //如果正确写入了cookie返回TRUE
    this.addCookie = function(cookieName,cookieValue) {
        var the_date = new Date("December 31, 2020");
        var expiresDate = the_date.toGMTString();
        document.cookie = cookieName+"=" + escape(cookieValue) + "; expires=" + expiresDate;
        return true ;
    };
    //检查客户端cookie是否打开,如果打开返回TRUE 否则返回FALSE
    this.getCookieStatus=function() {
        var status =false;
        var cookieStr = "wb_check=kcehc_bw";
        document.cookie = cookieStr;
        if (document.cookie.indexOf(cookieStr) > -1) {
            status = true;
            var date = new Date();
            date.setTime(date.getTime() - 1000);
            document.cookie = cookieStr + "; expires=" + date.toGMTString();
        }
        return status;
    };
   
    //判断是不是为正整数
    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;
    };
    // 判断字符串是否为Email地址,是返回true 否则返回false
    this.checkEmail = function(str) {
        if (/^[/w-]+(/.[/w-]+)*@[/w-]+(/.[/w-]+)+$/.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 ;
            }
        }
    };
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值