엄지월드

파일 유효성 체크하는 방법 본문

Front

파일 유효성 체크하는 방법

킨글 2017. 7. 20. 16:52
반응형

1. 전체적인 file type 제어

$('input[type="file"]').change(function () {

    var ext = this.value.match(/\.(.+)$/)[1].toLowerCase();

    switch (ext) {

        case 'jpg':

        case 'jpeg':

        case 'png':

        case 'gif':

        case 'war':

        case 'zip':

        case 'xlsx':

        case 'docx':

        case 'hwp':

            $('#uploadButton').attr('disabled', false); 

            break;

        default:

            alert('지원하지 않는 형식입니다');

            this.value = '';

    }

});


2. 부분적인 file type 제어 (id 사용)


if( $("#file").val() != "" ){

var ext = $('#file').val().split('.').pop().toLowerCase();

      if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {

alert('gif,png,jpg,jpeg 파일만 업로드 할수 있습니다.');

return;

      }

}


출처 : https://stackoverflow.com/questions/651700/how-to-have-jquery-restrict-file-types-on-upload

출처: http://beans9.tistory.com/165 [Beans9 : easy easy easy !!]



'Front' 카테고리의 다른 글

이미지 파일을 선택해서 이미지 미리보기  (0) 2017.09.13
jQuery 자동완성 기능(autoComplete)  (0) 2017.08.22
값으로서의 함수  (0) 2017.07.15
유효범위  (0) 2017.07.15
js 정규식  (0) 2017.07.15
Comments