1. 程式人生 > >PHP 校驗函式

PHP 校驗函式

 /**
*手機號碼校驗
**/
    function checkmobile($mobile)
    {
 
        $vMobile = trim($mobile);
 
        if (empty($vMobile))
        {
            echo '<script>alert("對不起,您輸入的手機號不能為空!");history.back();</script>';
            exit;
        }
 
        if (strlen($vMobile) != 11)
        {
            echo '<script>alert("對不起,您輸入的手機號不是11位!");history.back();</script>';
            exit;
        }
 
        $tel = preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/", $vMobile);
               
        if ($tel)
        {
            return $vMobile;
        } 
        else
        {
            echo '<script>alert("對不起,您輸入的手機號不正確!");history.back();</script>';
            exit;
        }
    }

---------------------------------------------------------------------------------------------------------------------------------------------------
     /**
      *校驗是不是數字 並取整 
      * 
      */ 
function checkCount($count)
     {  
        if(empty($count))
        {
           echo '<script>alert("對不起,您輸入的數量不能為空!");history.back();</script>'; exit; 
        }
        if(is_numeric($count))  //判斷不是數字
        {
            return intval($count);  //取整
        }
        else
        {
            echo '<script>alert("對不起,您輸入的數量不是數字!");history.back();</script>'; exit;
        }
     }
--------------------------------------------------------------------------------------------------------------------------------------------------
/** 郵箱驗證**/
function checkEmail($inAddress)
{
         return (ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+",$inAddress));
}
-------------------------------------------------------------------------------------------------------------------------------------------------
/*身份證校驗類*/
$Idcheck = new Idcheck('8998987987879');
var_dump($Idcheck->isIdNum());
 
class Idcheck {
 
    private $idNum;
 
    public function __construct($idNum) {
        $this->idNum = (string)$idNum;
    }
 
    public function isIdNum() {
        $idNum = $this->idNum;
        if (strlen($idNum) == 18) {
            return $this->idcardCheckSum18($idNum);
        } elseif ((strlen($idNum) == 15))    {
            $idNum = $this->idcard_15to18($idNum);
            return $this->idcardCheckSum18('"' . $idNum . '"');
        } else    {
            return false;
        }
    }
 
    // 計算身份證校驗碼,根據國家標準GB 11643-1999
    public function idcardVerifyNumber($idcardBase)
    {
        if(strlen($idcardBase) != 17)
        {
            return false;
        }
 
        //加權因子
        $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
 
        //校驗碼對應值
        $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        $checksum = 0;
        for ($i = 0; $i < strlen($idcardBase); $i++)
        {
            $checksum += substr($idcardBase, $i, 1) * $factor[$i];
        }
        $mod = $checksum % 11;
        $verify_number = $verify_number_list[$mod];
        return $verify_number;
    }
 
    // 將15位身份證升級到18位
    public function idcard_15to18($idcard){
        if (strlen($idcard) != 15){
            return false;
        }else{
            // 如果身份證順序碼是996 997 998 999,這些是為百歲以上老人的特殊編碼
            if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false){
            $idcard = substr($idcard, 0, 6) . '18'. substr($idcard, 6, 9);
            }else{
            $idcard = substr($idcard, 0, 6) . '19'. substr($idcard, 6, 9);
            }
        }
        $idcard = $idcard . idcard_verify_number($idcard);
        return $idcard;
    }
 
    // 18位身份證校驗碼有效性檢查
    public function idcardCheckSum18($idcard){
        if (strlen($idcard) != 18){ return false; }
        $idcard_base = substr($idcard, 0, 17);
        if ($this->idcardVerifyNumber($idcard_base) != strtoupper(substr($idcard, 17, 1))){
            return false;
        }else{
            return true;
        }
    }
}
--------------------------------------------------------------------------------------------------------------------------------------------------
 //檢查日期是否合法日期 
function check_date($date) {
$dateArr = explode("-", $date); 
if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) { 
return checkdate($dateArr[1],$dateArr[2],$dateArr[0]); 
} 
return false; 
} 
//檢查時間是否合法時間 
function check_time($time) {
$timeArr = explode(":", $time); 
if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) { 
if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59)) 
return true; 
else 
return false; 
} 
return false; 
} 
--------------------------------------------------------------------------------------------------------------------------------------------------