Script & jQuery
[javascript] 파일 확장자 및 파일 이름 체크
yeon.Biju
2021. 1. 27. 13:18
자바스크립트로 파일의 확장자 및 파일 이름을 체크할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<script type="text/javascript">
$(document).ready(function() {
$("input:file[name='file']").change(function () {
var str = $(this).val();
var fileName = str.split('\\').pop().toLowerCase();
//alert(fileName);
checkFileName(fileName);
});
});
function checkFileName(str){
//1. 확장자 체크
var ext = str.split('.').pop().toLowerCase();
if($.inArray(ext, ['bmp' , 'hwp', 'jpg', 'pdf', 'png', 'xls', 'zip', 'pptx', 'xlsx', 'jpeg', 'doc', 'gif']) == -1) {
//alert(ext);
alert(ext+'파일은 업로드 하실 수 없습니다.');
}
//2. 파일명에 특수문자 체크
var pattern = /[\{\}\/?,;:|*~`!^\+<>@\#$%&\\\=\'\"]/gi;
if(pattern.test(str) ){
//alert("파일명에 허용된 특수문자는 '-', '_', '(', ')', '[', ']', '.' 입니다.");
alert('파일명에 특수문자를 제거해주세요.');
}
}
</script>
|
cs |