欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

存储一些公用的函数

程序员文章站 2022-03-13 22:49:38
...

存储一些公用的函数

// js校验身份证的方法

function IdCodeValid(code){
    //身份证号合法性验证
    //支持15位和18位身份证号
    //支持地址编码、出生日期、校验位验证
    var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"* ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"*",71:"*",81:"香港",82:"澳门",91:"国外 "};
    var row={
        'pass':true,
        'msg':'验证成功'
    };
    if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(code)){
        row={
            'pass':false,
            'msg':'身份证号格式错误'
        };
    }else if(!city[code.substr(0,2)]){
        row={
            'pass':false,
            'msg':'身份证号地址编码错误'
        };
    }else{
        //18位身份证需要验证最后一位校验位
        if(code.length == 18){
            code = code.split('');
            //∑(ai×Wi)(mod 11)
            //加权因子
            var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
            //校验位
            var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
            var sum = 0;
            var ai = 0;
            var wi = 0;
            for (var i = 0; i < 17; i++)
            {
                ai = code[i];
                wi = factor[i];
                sum += ai * wi;
            }
            if(parity[sum % 11] != code[17].toUpperCase()){
                row={
                    'pass':false,
                    'msg':'身份证号校验位错误'
                };
            }
        }
    }
    return row;
}

    // 列表分页显示函数参数  
    $page:当前页 
    $pagesize:每页记录条数 
    $count:总记录条数 
    $url:页面地址
    Return:String

 function cs_page($page,$pagesize,$count,$url)
    {
        if ($page == '') { $page = 1; }
        if($count == 0) {
            $pagestr="<font color='red'>没有搜索到合适的记录!</font>";
            return $pagestr;
        }
        $pagenum = ceil($count / $pagesize);
        $pagestr="总计<font color=\"#FF0000\">".$count."</font>记录&nbsp;&nbsp;第<font color=\"#FF0000\">$page</font>/".$pagenum."页&nbsp;&nbsp;";
        
        if($page==1) {
            $pagestr.="首页&nbsp;&nbsp;上一页&nbsp;&nbsp;";
        } else {
            $pagestr.="<a href=\"".$url."\">首页</a>&nbsp;&nbsp;<a href=\"".$url."page=".($page-1)."\">上一页</a>&nbsp;&nbsp;";
        }
        
        if($page<$pagenum) {
            $pagestr.="<a href=\"".$url."page=".($page+1)."\">下一页</a>&nbsp;&nbsp;<a href=".$url."page=".$pagenum."\">尾页</a>";
        } else {
            $pagestr.="下一页&nbsp;&nbsp;尾页";
        }
        
        $pagestr.="&nbsp;&nbsp;<input name='pageno' id='pageno' value='$page' style='width:60px; text-align:center;' />";
        $pagestr.="<button name='pagebtn' type='button' onclick=\"javascript:location='".$url."page='+document.getElementById('pageno').value\">提交</button>";
        $pagestr.="<input type='hidden' id='pageurl' value='$url' />";
        return $pagestr;
    }
    
    
    
    /**

// Remarks:根据选择的时间类型计算 时间戳的范围【开始时间戳和结束时间戳】

function getTimeRange($sdata=array())
{
    $ntime = time(); //今天的时间戳
    $allow_timesets = [ 'today','yesterday','lastweek','thismonth','lastmonth','customize' ];
    $timeset = isset($sdata['timeset'])?$sdata['timeset']:'';
    $stime = isset($sdata['stime'])?$sdata['stime']:'';
    $etime = isset($sdata['etime'])?$sdata['etime']:'';
    // 时间搜索类型
    if( empty($timeset) ) { gourl('请选择时间类型', '', '-1'); }
    if( !in_array($timeset,$allow_timesets) ) { gourl('时间类型不再允许的查询之内', '', '-1'); }
    if( $timeset=='customize' && empty($stime) ) { gourl('请选择开始时间', '', '-1'); }
    if( $timeset=='today' ) // 今天的时间范围 - today
    {
        $stime_int = strtotime(date('Y-m-d', $ntime));
        $etime_int = strtotime(date('Y-m-d 23:59:59', $ntime));
    }
    else if( $timeset=='yesterday' ) // 昨天的时间范围 - yesterday
    {
        $ytime = strtotime("-1 day"); //昨天的时间戳
        $stime_int = strtotime(date('Y-m-d', $ytime));
        $etime_int = strtotime(date('Y-m-d 23:59:59', $ytime));
    }
    else if( $timeset=='thisweek') //本周周一到周日日的时间戳范围 - thisweek
    {
        $stime_int = strtotime(date('Y-m-d', strtotime("this week Monday", $ntime)));
        $etime_int = strtotime(date('Y-m-d', strtotime("this week Sunday", $ntime))) + 24 * 3600 - 1;
    }
    else if( $timeset=='lastweek' ) // 过去7天的的时间戳范围 - lastweek
    {
        $lwtime = strtotime('-6 day');
        $stime_int = strtotime(date('Y-m-d 0:0:0', $lwtime));
        $etime_int = $ntime;
    }
    else if( $sdata['timeset']=='thismonth' ) // 这个月的时间戳范围 - thismonth
    {
        $stime_int = strtotime(date('Y-m-01 0:0:0', $ntime));
        $etime_int = strtotime(date('Y-m-t 23:59:59', $ntime));
    }
    else if( $sdata['timeset']=='lastmonth' ) // 上个月的时间戳范围 - lastmonth
    {
        $month = date('m')-1;
        $mtime = strtotime(date("Y-$month"));
        $stime_int = strtotime(date("Y-m-01 0:0:0",$mtime));
        $etime_int = strtotime(date("Y-m-t 23:59:59",$mtime));
    }
    else if( $timeset=='customize' ) // 自定义的时间戳范围 - customize
    {
        $stime_int = strtotime($stime);
        $etime_int = 0;
        if( $stime==$etime || empty($etime) ) {
            $etime_int = strtotime($stime.' 23:59:59');
        } else {
            $etime_int = strtotime($etime.' 23:59:59');
        }
        if( $stime_int > $etime_int ) {
            gourl('选择的开始时间不能大于结束时间', '', '-1');
        }
    }
    return [$stime_int, $etime_int];
}
相关标签: 函数