Programming/JavaScript & jQuery

File 폼 확장자 체크하기

DOTI 2015. 12. 7. 10:17
File 폼 확장자 체크하기
반응형

파일 폼 업로드전 jQuery를 이용하여 확장자를 체크할 수 있다


[예제]

1
2
3
4
5
6
7
8
9
10
11
12
13
<input type="file" id="file">
 
<script>
$("#file").change(function(){
    if( $(this).val() != "" ){
    var ext = $(this).val().split('.').pop().toLowerCase();
        if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
            alert('gif,png,jpg,jpeg 파일만 업로드 할수 있습니다.');
            return;
        }
    }
})    
</script>
cs


위의 예제에서는 file 이라는 ID 값을 가진 폼을 업로드 전 확장자 체크를 하여 정해진 확장자가 아니면 메시지를 띄우는 스크립트이다

반응형