2010年2月17日水曜日

phpで入力値の判定

require_once("MySmarty.class.php");
class CheckUtil {
    //エラーのプライベート変数
private $error;
    //からの配列で初期化
    function __construct(){
        $this->error=array();
    }
    //エラー情報を読み取る
    public function getError(){
        return $this->error;
    }
    //エラー情報を書き込む
    public function setError($strErr){
        $this->error[]=$strErr;
    }
//showResult()関数でエラー画面に表示する
   public function showResult(){
        if(count($this->error)>0){
            $o_smarty=new MySmarty();
//            $o_smarty->template_dir="../templates";
            $o_smarty->assign("errors",$this->error);
            $o_smarty->display("error.tpl");
            exit();
        }
    }
//trimは先頭および末尾の空白文字を取り除く
//===は型の比較を行っています。
    public function requiredCheck($strVal,$strErr) {
        if(is_null($strVal) || trim($strVal)==''){
            $this->error[]=$strErr."は必須入力です";
        }
    }
    public function lengthCheck($strVal,$intMax,$strErr){
        if(is_null($strVal)===FALSE && trim($strVal)!=''){
            if(mb_strlen($strVal)>$intMax){
                $this->error[]=$strErr."は".$intMax."桁以下で入力してください";
            }
        }
    }
    public function ZenCheck($strVal,$strErr){
        if(is_null($strVal)===FALSE && trim($strVal)!=''){
            if(mb_strlen($strVal)*2!=strlen($strVal)){
                $this->error[]=$strErr."は全角(2バイト文字)で入力してください";
            }
        }
    }
    public function HanCheck($strVal,$strErr){
        if(is_null($strVal)===FALSE && trim($strVal)!=''){
            if(mb_strlen($strVal)!=strlen($strVal)){
                $this->error[]=$strErr."は半角(1バイト文字)で入力してください";
            }
        }
    }
    public function numberTypeCheck($strVal,$strErr){
        if(is_null($strVal)===FALSE && trim($strVal)!=''){
            if(is_numeric($strVal)===FALSE){
                $this->error[]=$strErr."は数値で入力してください";
            }
        }
    }
    public function dateTypeCheck($strVal,$strErr){
        if(is_null($strVal)===FALSE && trim($strVal)!=''){
            if(!ereg("^[0-9]{4}/[0-9]{1,2}/[0-9]{1,2}$",$strVal)){
                $this->error[]=$strErr."は日付形式で入力してください";
            } else{
                $aryStr=split("/",$strVal);
                if($strVal!=
                    date("Y/m/d",mktime(0,0,0,$aryStr[1],$aryStr[2],$aryStr[0]))){
                    $this->error[]=$strErr."は正しい日付で入力してください";
                }
            }
        }
    }
    public function dateNumberTypeCheck($strYear,$strMonth,$strDay,$strErr){
        if(is_numeric($strYear)===FALSE ||
            is_numeric($strMonth)===FALSE || is_numeric($strDay)===FALSE){
            $this->error[]=$strErr."は数値で入力してください";
        }
        $intTmp=mktime(0,0,0,$strMonth,$strDay,$strYear);
        if(date("Y",$intTmp)!=$strYear ||
            date("m",$intTmp)!=$strMonth || date("d",$intTmp)!=$strDay){
            $this->error[]=$strErr."は正しい日付形式で入力してください";
        }
    }
    public function rangeCheck($strVal,$intMax,$intMin,$strErr){
        if(is_null($strVal)===FALSE && trim($strVal)!=''){
            if(is_numeric($strVal)===FALSE){
                $this->error[]=$strErr."は数値で入力してください";
            }
            if($strVal<$intMin || $strVal>$intMax){
                $this->error[]=$strErr."は".$intMin."以上、かつ"
                    .$intMax."以下で入力してください";
            }
        }
    }
    public function regExCheck($strVal,$strPtn,$strErr){
        if(is_null($strVal)===FALSE && trim($strVal)!=''){
            if(!ereg($strPtn,$strVal)){
                $this->error[]=$strErr."を正しい形式で入力してください";
            }
        }
    }
//二つの数字に大小の比較compareCheck()
   public function compareCheck($strVal1,$strVal2,$strErr1,$strErr2){
        if(is_null($strVal1)===FALSE && trim($strVal1)!=''
            && is_null($strVal2)===FALSE && trim($strVal2)!=''){
            if($strVal1>=$strVal2){
                $this->error[]=$strErr1."は".$strErr2."より小さい値を指定してください";
            }
        }
    }
//duplicateCheck()はsqlとphpを連係して、データベースでのある値の存在チェック
    public function duplicateCheck($sql,$strErr) {
        $db=new mysqli("localhost","php","php","sample");
        $rs=$db->query($sql);
        if(!is_null($rs->fetch_array(MYSQLI_ASSOC))){
            $this->error[]=$strErr."が重複しています";
        }
    }
}
?>

0 件のコメント:

コメントを投稿