1. 程式人生 > >前後端驗證個人實踐

前後端驗證個人實踐

備註:

利用jQuery

前端:

// xxxxxxx/manageLanguageDialog.tpl

//Validate all data length and type
function validateDataLengthAndType(selector, length, type, messageSelector = null){
    var pattern = /^[0-9]*$/;
    var selectorElement= $('#' + selector);
    var messageSelector = '.' + selector + messageSelector;
    switch(type)
    {
        case 'int':
            if(!pattern.test(selectorElement.val()) || parseInt(selectorElement.val()) >  2147483647){
                $(messageSelector).show();
                validateResult++;
            }
            break;
        case 'tinyint':
            switch(length){
                case 1:
                    if((selectorElement.val() !== '0' && selectorElement.val() !== '1')){
                        $(messageSelector).show();
                        validateResult++;
                    }
                    break;
                case 4:
                    if(!pattern.test(selectorElement.val()) || selectorElement.val() > 255){
                        $(messageSelector).show();
                        validateResult++;
                    }
                    break;
                default:
                    break;
            }
            break;
        case 'varchar':
            if(typeof selectorElement.val() !== 'string' || selectorElement.val().length > length){
                $(messageSelector).show();
                validateResult++;
            }
            break;
        case 'char':
            if(typeof selectorElement.val() !== 'string' || selectorElement.val().length !== length){
                $(messageSelector).show();
                validateResult++;
            }
            break;
        default:
            console.log('error: no this type !');
            break;
    }
    return validateResult;
}

// xxxxxxx//language_management.tpl

revalidation();
var allDataList = getDialogInputsData();
console.log(allDataList);
for (var key in allDataList){
    if((allDataList[key] == '' || allDataList[key] == 'NULL') && allDataList[key] != 'language_id' && allDataList[key] != 'name'){
        continue;
    }
    //int(11) amount:3
    if(key == 'language_id' || key == 'storm_language_id' || key == 'lts_lang_id'){
        validateResult = validateDataLengthAndType(key, 11, 'int', '.message_int_11');
    }
    //varchar(120) amount:3
    if(key == 'name' || key == 'lts_lang_name' || key == 'wonderland_lang_name'){
        validateResult = validateDataLengthAndType(key, 120, 'varchar', '.message_varchar_120');
    }
    //varchar(10) amount:6
    if(key == 'disney_code' || key == 'dcin_anno' || key == 'pixar_lang_code' || key == 'dcin_iop_subtitle' || key == 'dcin_smpte_subtitle' || key == 'itunes_lang_code'){
        validateResult = validateDataLengthAndType(key, 10, 'varchar', '.message_varchar_10');
    }
    //char(12) amount:1
    if(key == 'language'){
        validateResult = validateDataLengthAndType(key, 12, 'char', '.message_char_12');
    }
    //varchar(5) amount:1
    if(key == 'locale'){
        validateResult = validateDataLengthAndType(key, 5, 'varchar', '.message_varchar_5');
    }
    //varchar(3) amount:1
    if(key == 'storm_code'){
        validateResult = validateDataLengthAndType(key, 3, 'varchar', '.message_varchar_3');
    }
    //tinyint(1) amount:7
    if(key == 'metadata_valid' || key == 'audio_valid' || key == 'timedtext_valid' || key == 'timedtext_cctt_valid' || key == 'isactive' || key == 'lir_valid' || key == 'dtc_language'){
        validateResult = validateDataLengthAndType(key, 1, 'tinyint', '.message_tinyint_1');
    }
    //tinyint(4) amount:4
    if(key == 'trailer_valid' || key == 'artwork_valid' || key == 'priority' || key == 'dubcard_valid'){
        validateResult = validateDataLengthAndType(key, 4, 'tinyint', '.message_tinyint_4');
    }
    //varchar(255) amount:5
    if(key == 'note' || key == 'dcp_aud_lang' || key == 'dcp_sub_lang' || key == 'sonar_lang_name' || key == 'sonar_lang_code'){
        validateResult = validateDataLengthAndType(key, 255, 'varchar' ,'.message_varchar_255');
    }
    //varchar(32) amount:1
    if(key == 'simple_name'){
        validateResult = validateDataLengthAndType(key, 32, 'varchar', '.message_varchar_32');
    }
    //varchar(20) amount:2
    if(key == 'bolt_lang_code' || key == 'dcp_lang_code'){
        validateResult = validateDataLengthAndType(key, 20, 'varchar', '.message_varchar_20');
    }
}
if(validateResult > 0){
    return;
}

後端

// xxxxxxx//language_management.php

foreach ($data as $key => $value){
    if(empty($value) && $key !== 'language_id' && $key !== 'name')
        continue;
    //int(11) amount:3
    if($key == 'id' || $key == 'storm_language_id' || $key == 'lts_lang_id'){
        $result = $this->validateDataLengthAndType($key, $value, 11, 'int');
        if($result > 0)
            return;
    }
    //varchar(120) amount:3
    if($key == 'name' || $key == 'lts_lang_name' || $key == 'wonderland_lang_name'){
        $result = $this->validateDataLengthAndType($key, $value, 120, 'varchar');
        if($result > 0)
            return;
    }
    //varchar(10) amount:6
    if($key == 'disney_code' || $key == 'dcin_anno' || $key == 'pixar_lang_code' || $key == 'dcin_iop_subtitle' || $key == 'dcin_smpte_subtitle' || $key == 'itunes_lang_code'){
        $result = $this->validateDataLengthAndType($key, $value, 10, 'varchar');
        if($result > 0)
            return;
    }
    //char(12) amount:1
    if($key == 'language'){
        $result = $this->validateDataLengthAndType($key, $value, 12, 'char');
        if($result > 0)
            return;
    }
    //varchar(5) amount:1
    if($key == 'locale'){
        $result = $this->validateDataLengthAndType($key, $value, 5, 'varchar');
        if($result > 0)
            return;
    }
    //varchar(3) amount:1
    if($key == 'storm_code'){
        $result = $this->validateDataLengthAndType($key, $value, 3, 'varchar');
        if($result > 0)
            return;
    }
    //tinyint(1) amount:7
    if($key == 'metadata_valid' || $key == 'audio_valid' || $key == 'timedtext_valid' || $key == 'timedtext_cctt_valid' || $key == 'isactive' || $key == 'lir_valid' || $key == 'dtc_language'){
        $result = $this->validateDataLengthAndType($key, $value, 1, 'tinyint');
        if($result > 0)
            return;
    }
    //tinyint(4) amount:4
    if($key == 'trailer_valid' || $key == 'artwork_valid' || $key == 'priority' || $key == 'dubcard_valid'){
        $result = $this->validateDataLengthAndType($key, $value, 4, 'tinyint');
        if($result > 0)
            return;
    }
    //varchar(255) amount:5
    if($key == 'note' || $key == 'dcp_aud_lang' || $key == 'dcp_sub_lang' || $key == 'sonar_lang_name' || $key == 'sonar_lang_code'){
        $result = $this->validateDataLengthAndType($key, $value, 255, 'varchar');
        if($result > 0)
            return;
    }
    //varchar(32) amount:1
    if($key == 'simple_name'){
        $result = $this->validateDataLengthAndType($key, $value, 32, 'varchar');
        if($result > 0)
            return;
    }
    //varchar(20) amount:2
    if($key == 'bolt_lang_code' || $key == 'dcp_lang_code'){
        $result = $this->validateDataLengthAndType($key, $value, 20, 'varchar');
        if($result > 0)
            return;
    }
}
/*Validate data length and type function*/
public function validateDataLengthAndType($key, $value, $length = 255, $type = 'varchar'){
    $result = 0;
    if(empty($key) || empty($value) || $value == 'NULL')
        return $result;
    $pattern = '/^[0-9]+$/';
    switch ($type){
        case 'int':
            if(!preg_match($pattern, $value) || !((int)$value <= 2147483647)){
                $result++;
            }
            break;
        case 'tinyint':
            switch ($length){
                case 1:
                    if($value !== '1' && $value !== '0'){
                        $result++;
                    }
                    break;
                case 4:
                    if(!preg_match($pattern, $value) || !((int)$value <= 255)){
                        $result++;
                    }
                    break;
                default:
                    $result++;
                    break;
            }
            break;
        case 'varchar':
            if(!(mb_strlen($value) < $length) || !is_string($value)){
                $result++;
            }
            break;
        case 'char':
            if((mb_strlen($value) !== $length) || !is_string($value)){
                $result++;
            }
            break;
        default:
            jQuery::addData('params', ['inputDefault' => true,"column" => $key], 'saveLanguage_callback');
            $result++;
    }
    return $result;
}

 

後言:

仍可以繼續提取封裝,暫時如此.提供個思路.

關於數字型別的準確檢測,發現只能用到正則進行匹配.