常用工具函数

// 数组移除元素
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

// 清除所有cookie
function clearAllCookie() {
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if(keys) {
        for(var i = keys.length; i--;){
            document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
        }
    }
}

//bootstrap-table 页面跳转
function toPage() {
    var pageNum = $("#pageNum").val();
    if (pageNum) {
        $('#nurseTable').bootstrapTable('selectPage', parseInt(pageNum));
    }
}

// 遮罩以及内容框弹出
//兼容火狐、IE8
function showMask(mask){
        $(mask).css("height",$(document).height());
        $(mask).css("width",$(document).width());
        $(mask).show();
}
//让指定的DIV始终显示在屏幕正中间
function letDivCenter(divName){
        var top = ($(window).height() - $(divName).height())/2;
        var left = ($(window).width() - $(divName).width())/2;
        var scrollTop = $(document).scrollTop();
        var scrollLeft = $(document).scrollLeft();
        $(divName).css( { position : 'absolute', 'top' : top + scrollTop, left : left + scrollLeft } ).show();
}
function showAll(divName, mask){
        showMask(mask);
        letDivCenter(divName);
}

// ie9及以下浏览器兼容placeholder
function placeholder(target){
    // var browser=navigator.appName
    // var b_version=navigator.appVersion
    // var version=b_version.split(";");
    // var trim_Version=version[1].replace(/[ ]/g,"");
    // if(browser=="Microsoft Internet Explorer" && trim_Version=="MSIE7.0" || browser=="Microsoft Internet Explorer" && trim_Version=="MSIE8.0" || browser=="Microsoft Internet Explorer" && trim_Version=="MSIE9.0"){
        $(target).siblings(".spn").show();
        $(target).focus(function() {
            $(this).siblings(".spn").hide();
        })
        $(target).blur(function(){
            if($(this).val() == "") {
                $(this).siblings(".spn").show();
            }
        })
    // }
}

// 获取url中的参数值
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

//将时间戳转换为日期
function timestampToTime(timestamp) {
    var Y, M, D, h, m, s;
    var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
    Y = date.getFullYear() + '-';
    M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
    h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':'
    m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':'
    s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s;
}

//正则判断身份证
function isCardNo(card) {
    var pattern = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/;
    return pattern.test(card);
}

// 正则校验手机号和座机号
function isPhone(mobile) {
    var isPhone = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/; //手机正则
    var isMob= /^((0\d{2,3})-?)(\d{7,8})(-(\d{3,}))?$/;// 座机
    // console.log('isPhone:' + isPhone.test(mobile))
    // console.log('isMob:' + isMob.test(mobile))
    if(isPhone.test(mobile) || isMob.test(mobile)){
        return true
    }else{
        return false
    }
}

//正则校验手机好
function istelphone(phone){
    var isPhone = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/; //手机正则
    if(isPhone.test(phone)){
        return true
    }else{
        return false
    }
}

// 名字为中文和英文时通过
function petientName(name){
    var pattern = /^[\u4E00-\u9FA5A-Za-z]+$/;
    return pattern.test(name)
}

// 根据身份证号算出出生日期
function getBirthdayFromIdCard(idCard) {
    var birthday = "";
    if (idCard != null && idCard != "") {
        if (idCard.length == 15) {
            birthday = "19" + idCard.substr(6, 6);
        } else if (idCard.length == 18) {
            birthday = idCard.substr(6, 8);
        }
        birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
    }
    return birthday;
}

// 根据身份证号算出年龄
function getAge(identityCard) {
    var len = (identityCard + "").length;
    if (len == 0) {
        return '';
    } else {
        if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
        {
            return '';
        }
    }
    var strBirthday = "";
    if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
    {
        strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
    }
    if (len == 15) {
        strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
    }
    //时间字符串里,必须是“/”
    var birthDate = new Date(strBirthday);
    var nowDateTime = new Date();
    var age = nowDateTime.getFullYear() - birthDate.getFullYear();
    //再考虑月、天的因素;.getMonth()获取的是从0开始的,这里进行比较,不需要加1
    if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}

/*根据出生日期算出年龄*/
function jsGetAge(strBirthday) {
    if((strBirthday == '') || (strBirthday == undefined)){
        return '';
    }
    // console.log(strBirthday)
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];

    d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();

    if (nowYear == birthYear) {
        returnAge = 0; //同年 则为0岁
    } else {
        var ageDiff = nowYear - birthYear; //年之差
        if (ageDiff > 0) {
            if (nowMonth == birthMonth) {
                var dayDiff = nowDay - birthDay; //日之差
                if (dayDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            } else {
                var monthDiff = nowMonth - birthMonth; //月之差
                if (monthDiff < 0) {
                    returnAge = ageDiff - 1;
                } else {
                    returnAge = ageDiff;
                }
            }
        } else {
            returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
        }
    }

    return returnAge; //返回周岁年龄
    // console.log(returnAge)

}



// 判断浏览器类型
function BrowserType() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    // console.log(userAgent)
    var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
    var isEdge = userAgent.indexOf("Windows NT 6.1;") > -1 && userAgent.indexOf("Trident/7.0;") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
    var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
    var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器

    // console.log(isEdge)
    if (isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion == 7) { return "IE7"; } else if (fIEVersion == 8) { return "IE8"; } else if (fIEVersion == 9) { return "IE9"; } else if (fIEVersion == 10) { return "IE10"; } else if (fIEVersion == 11) { return "IE11"; } else { return "0" } //IE版本过低
    } //isIE end

    if (isFF) { return "FF"; }
    if (isOpera) { return "Opera"; }
    if (isSafari) { return "Safari"; }
    if (isChrome) { return "Chrome"; }
    if (isEdge) { return "Edge"; }
}



var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];    // 加权因子
var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];            // 身份证验证位值.10代表X

function IdCardValidate(idCard) {
    idCard = trim(idCard.replace(/ /g, ""));
    // idCard = maleOrFemalByIdCard(idCard)         //去掉字符串头尾空格
    if (idCard.length == 15) {
        return isValidityBrithBy15IdCard(idCard);       //进行15位身份证的验证
    } else if (idCard.length == 18) {
        var a_idCard = idCard.split("");                // 得到身份证数组
        if(isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)){
        //进行18位身份证的基本验证和第18位的验证
            // console.log('true')
            return true;
        }else {
            // console.log('false')
            return false;
        }
    } else {
        // console.log('false')
        return false;
    }
}
/**
 * 判断身份证号码为18位时最后的验证位是否正确
 * @param a_idCard 身份证号码数组
 * @return
*/
function isTrueValidateCodeBy18IdCard(a_idCard) {
var sum = 0;                             // 声明加权求和变量
    if (a_idCard[17].toLowerCase() == 'x') {
        a_idCard[17] = 10;                    // 将最后位为x的验证码替换为10方便后续操作
    }
for ( var i = 0; i < 17; i++) {
        sum += Wi[i] * a_idCard[i];            // 加权求和
    }
    valCodePosition = sum % 11;                // 得到验证码所位置
    if (a_idCard[17] == ValideCode[valCodePosition]) {
return true;
    } else {
return false;
    }
}
/**
  * 验证18位数身份证号码中的生日是否是有效生日
  * @param idCard 18位书身份证字符串
  * @return
*/
function isValidityBrithBy18IdCard(idCard18){
var year =  idCard18.substring(6,10);
var month = idCard18.substring(10,12);
var day = idCard18.substring(12,14);
var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
// 这里用getFullYear()获取年份,避免千年虫问题
    if(temp_date.getFullYear()!=parseFloat(year)
          ||temp_date.getMonth()!=parseFloat(month)-1
          ||temp_date.getDate()!=parseFloat(day)){
return false;
    }else{
return true;
    }
}
/**
   * 验证15位数身份证号码中的生日是否是有效生日
   * @param idCard15 15位书身份证字符串
   * @return
*/
function isValidityBrithBy15IdCard(idCard15){
var year =  idCard15.substring(6,8);
var month = idCard15.substring(8,10);
var day = idCard15.substring(10,12);
var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));
// 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法
      if(temp_date.getYear()!=parseFloat(year)
              ||temp_date.getMonth()!=parseFloat(month)-1
              ||temp_date.getDate()!=parseFloat(day)){
        // console.log(false)
            return false;
        }else{
            // console.log(true)
            return true;
        }
  }
//去掉字符串头尾空格
function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}
// 根据身份证号对其进行性别的判定

/**
 * 通过身份证判断是男是女
 * @param idCard 15/18位身份证号码
 * @return 'female'-女、'male'-男
*/
function maleOrFemalByIdCard(idCard){
    idCard = trim(idCard.replace(/ /g, ""));        // 对身份证号码做处理。包括字符间有空格。
if(idCard.length==15){
if(idCard.substring(14,15)%2==0){
return 'female';
        }else{
return 'male';
        }
    }else if(idCard.length ==18){
if(idCard.substring(14,17)%2==0){
return 'female';
        }else{
return 'male';
        }
    }else{
return null;
    }
}

//五次放缓动
//elem  运动元素,prop 运动方向 left top bottom ,target 目标 1000,speed 速度 200ms
//示例  ownerAnimate($('#animate')[0],"left",0,500);
function ownerAnimate(elem,prop,target,speed,callBack){
    if(elem.timer) clearTimeout(elem.timer);
        var begin = parseInt(getStyle(elem,prop));
        var change = target - begin;
        //test.innerHTML  = speed;
        var duration = speed;
        var time = 0;
        Move();
        function Move(){
            time += 5;
            //test.innerHTML = time;
            elem.style[prop] = easeOutCubic(time,begin,change,duration)+"px";
            if(time<duration){
                elem.timer = setTimeout(Move,20);
            }
            else{
                if(callBack) callBack();
            }
        }
}

//获取样式
function getStyle(obj,name){
    if(obj.currentStyle){   return obj.currentStyle[name];  }
    else{ return document.defaultView.getComputedStyle(obj,null)[name];}
}

//easeOutCubic动画,参数含义:time(取值[0-d]),begin,change,duration
function easeOutCubic(t, b, c, d) {
    return Math.ceil(c*((t=t/d-1)*t*t + 1) + b);
}

//easeOutQuint5次方缓动曲线,备用
function easeOutQuint(t,b,c,d){
    var rs = c*((t=t/d-1)*t*t*t*t + 1) + b;
    return Math.ceil(rs);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值