JS 电话、网址、邮箱校验,cookie操作,图片等比缩放

常用的JS方法,文本格式检验、地址参数获取、cookie操作、图片登陆缩放等,可作为js公用方法使用

//检查电话号码    
function CheckMobilephone(num) {
    var check = /^(1[3-9]{1,}\d{9})$/;
    var re = new RegExp(check);
    if (re.exec(num)) {
        return true;
    }
    return false;
}
//检查固定电话号码
function CheckTelePhone(num) {
    var check = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
    var re = new RegExp(check);
    if (re.exec(num)) {
        return true;
    }
    return false;
}
//检查网址格式
function CheckUrl(num) {
    var check = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)?(([A-Za-z0-9-~]+)\.)+([A-Za-z0-9-~\/])+$/;
    var re = new RegExp(check);
    if (re.exec(num)) {
        return true;
    }
    return false;
}
//检查邮箱格式
function CheckEmail(num) {
    var check = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var re = new RegExp(check);
    if (re.exec(num)) {
        return true;
    }
    return false;
}
//获取地址栏参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

//写cookies 
function setCookie(name, value, second) {
    if (!second) {
        second = 7 * 24 * 60 * 60;//默认7天
    }
    var exp = new Date();
    exp.setTime(exp.getTime() + second * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
//读取cookies 
function getCookie(name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(name + "=");//获取字符串的起点
        if (c_start != -1) {
            c_start = c_start + name.length + 1;//获取值的起点
            c_end = document.cookie.indexOf(";", c_start);//获取结尾处
            if (c_end == -1) c_end = document.cookie.length;//如果是最后一个,结尾就是cookie字符串的结尾
            return decodeURI(document.cookie.substring(c_start, c_end));//截取字符串返回
        }
    }
    return "";
}
//删除cookies 
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
//验证整数
function checkNum(obj) {
    if (/[^\d]/.test(obj.value)) {
        obj.value = obj.value.replace(/[^\d]/g, '');
    }
}
//限制输入浮点数
function LimitDecimal(obj) {
    if (isNaN(obj.value)) {
        document.execCommand('undo');
    }
}
//限制输入整数
function LimitInt(obj) {
    obj.value = obj.value.replace(/\D/g, '');
}
//限制输入电话号码加“-”
function LimitTel(obj) {
    obj.value = obj.value.replace(/[^\d^-]/g, '');
}
//字符串截取并加...
function Cutstr(s,num) {
    if (s.length >= num) {
        return s.substr(0, num)+'...';
    } else {
        return s;
    }
}
//图片加载等比缩放
function DrawImage(ImgD, iwidth, iheight) {
    //参数(图片,允许的宽度,允许的高度)
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        flag = true;
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.width = iwidth;
                ImgD.height = (image.height * iwidth) / image.width;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        } else {
            if (image.height > iheight) {
                ImgD.height = iheight;
                ImgD.width = (image.width * iheight) / image.height;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
    }
}
//一个事件一天只执行一次
function DoOne(key,method){
    //存一个cookie,有效期是第二天凌晨
    var showEdit = getCookie(key);
    if (!showEdit) {
        method();
        //获取第二天凌晨到当前时间的秒数
        var tim_sec = 24 * 60 * 60 - (new Date().getHours() * 60 * 60 + new Date().getMinutes() * 60 + new Date().getSeconds());
        setCookie(key, "1", tim_sec);
    }
}

 

你可能感兴趣的:(JS)