본문 바로가기

Development/JavaScript

JavaScript:: 파일명 검사


1. 정규표현식 파일명체크

function extCheck(filename) { 
if (filename.match(/\\.(gif|jpg|jpeg|png|bmp)$/i)) return true; 


2. 기본문법을 이용한 파일명 검사
function chk_img(img_path) {
        
        var f = document.theForm;
        var file = img_path;
        var xfile = file.substring(file.lastIndexOf(".") + 1);
          
         // substring 첫번째 인자부터 두번째 인자까지의 값을 반환
        
        if (xfile.toLowerCase()!="jpg"){
           alert(xfile.toUpperCase() + " 파일은 업로드 할 수 없습니다.\n이미지 파일만 업로드할 수 있습니다."); 
           
           //document.theForm.menu_img.select();
           
           //document.selection.clear();
 
         }else{
            
        return true;
        
        }
    }