---
title: "几个常用的js 函数汇总大全"
author: "cczzyc"
date: "2015-12-09T16:08:32Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2015-12-09T16:08:32Z"
id: 2262931
url: "https://hardworkbird.iteye.com/blog/2262931"
---

废话不多说，直接看代码：

1.非空验证

function isNotEmpty(str){

if(str==null)

{

return false;

}

if(typeof(str)=='undefined'){

return false;

}

if(str.replace("/(^s\*)|(s\*$)/g", "").length ==0){

return false;

}

return true;

}

2.    //跳过工作日后N天：即当前日期后n天，有周六周末则跳过不算在n内  
    function getWorkNDayAfter(datetime,num){  
        var date = new Date(datetime);  
        date = new Date(+date + 1000\*60\*60\*24);  
        if(date.getDay()%6 !=0){  
            num--;  
        }  
        if(num&lt;=0){  
            return date;  
        }else{  
            return getWorkNDayAfter(date,num);  
        }  
    }

3.    //屏蔽空格和非数字  
    function formatInput(srcEle){  
        var \_val = srcEle.value;  
        \_val = \_val.replace(/\\s/g,'');  
        \_val = \_val.replace(/\[\\D]/g,'');  
        srcEle.value = \_val;  
    }

4.//下月第一天  
    function showMonthFirstDay()  
    {  
        var applyDate = $("#deduct\_date").val();  
        var Nowdate=new Date(applyDate);  
        Nowdate.setDate(Nowdate.getDate()+1);  
        return Nowdate;  
    }  
     
    //下周第一天   
    function showNextFirstWeekDay()   
    {   
        var applyDate = $("#deduct\_date").val();  
        var Nowdate=new Date(applyDate);  
        var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)\*86400000);   
        var WeekLastDay=new Date((WeekFirstDay/1000+6\*86400)\*1000);  
        return new Date((WeekLastDay/1000+86400)\*1000);  
    }  
5.//增加身份证验证  
        function isIdCardNo(num) {  
            // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X   
               var reg = /(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X)$)/;  
               var cert\_type=$('#c\_cert\_type').val();  
               if(cert\_type=='1'){  
                   if(reg.test(num) === false)   
                   {   
                       return  false;   
                   }  
                   var tmpStr = "";  
                   if (num.length == 15) {  
                       tmpStr = num.substring(6, 12);  
                       tmpStr = "19" + tmpStr;  
                       tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)  
                   } else {  
                       tmpStr = num.substring(6, 14);  
                       tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)  
                   }  
                   if(!tmpStr.isDate()){  
                       $("#c\_birthday").val("");  
                       return false;  
                   }  
                   $("#c\_birthday").val(tmpStr);  
                   if(num.substring(num.length-2,num.length-1)%2==0){  
                       jsSelectItemByValue(document.getElementById("c\_sex"),2);  
                   }else{  
                       jsSelectItemByValue(document.getElementById("c\_sex"),1);  
                   }  
                   if (num.length == 18){  
                       var arrExp = \[7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//加权因子   
                       var arrValid = \[1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//校验码   
                       var sum = 0, idx;   
                       for(var i = 0; i &lt; num.length - 1; i++){   
                           // 对前17位数字与权值乘积求和   
                           sum += parseInt(num.substr(i, 1), 10) * arrExp\[i];   
                       }   
                       // 计算模（固定算法）   
                       idx = sum % 11;   
                       // 检验第18为是否与校验码相等   
                       return arrValid\[idx] == num.substr(17, 1).toUpperCase();   
                   }  
               }  
               return true;  
        }  
         
        //邮编验证  
        function industry(num) {  
           var reg = /(^\[0-9]{0,8}$)|(^无$)/;   
           if(reg.test(num) === false)   
           {   
               return  false;   
           }  
           return true;  
        }

//手机号码验证  
        function isMobile(num) {  
           var reg = /^1\[3-8]\\d{9}$/;   
           if(reg.test(num) === false)   
           {   
               return  false;   
           }  
           return true;  
        }