Restoration
10/28/2016 - 8:17 AM

バリデーション

バリデーション

<?php
/*
 * 入力された文字列がすべてカタカナか判定
 *
 * param String
 * return Boolean 
 **********/
function validate_kana($str){
    if(preg_match("/^(\xe3\x82[\xa1-\xbf]|\xe3\x83[\x80-\xbe]|".
                "\xa5[\xa1-\xf6]|\xa1[\xb3\xb4\xbc]|".
                "\x83[\x40-\x96]|\x81[\x52\x53\x5b])+$/",$str)){
                    return true;
    }else{
        return false;
    }
}

/*
 * 正しい形式の郵便番号であるか判定
 * ハイフンが必須の場合はfalse
 *
 * param String
 * param Boolean
 * return Boolean 
 **********/
function validate_postalcode($str,$flg = true) {
    if($flg){
        //ハイフンがあってもなくてもよい
        if (preg_match('/^[0-9]{3}[\s-]?[0-9]{4}$/',$str)) {
            return true;
        } else {
            return false;
        }
    } else {
        //ハイフン必須
        if (preg_match('/^[0-9]{3}-[0-9]{4}$/',$str)) {
            return true;
        } else {
            return false;
        }
    }
}
    
/*
 * 正しい形式の電話番号であるか判定
 * ハイフンが必須の場合はfalse
 *
 * param String
 * param Boolean
 * return Boolean 
 **********/
function validate_tel($number,$flg = true) {
    if($flg){
        //ハイフンがあってもなくてもよい
        if (preg_match('/^(0\d{1,4}[\s-]?\d{1,4}[\s-]?\d{4})$/',$number)) {
            return true;
        } else {
            return false;
        }
    } else {
        //ハイフン必須
        if (preg_match('/^(0\d{1,4}-\d{1,4}-\d{4})$/',$number)) {
            return true;
        } else {
            return false;
        }
    }
}   
?>