Javascript function to check the extension of file in the url. This is useful for setting filters for file control. the function and it sample use is given below:
function checkFileType(str){
file_name=str.replace(/^.*(\\|\/|\:)/, '');
ext=file_name.split('.').pop().toLowerCase();
if( ext!= 'doc' && ext!= 'jpg' && ext!= 'jpeg' && ext!= 'png' && ext!= 'gif' && ext!= 'pdf'){
alert("Selected file "+file_name+" is invalid, Please upload file of type doc,jpg,jpeg,pdf,gif or png. ");
document.getElementById('file').value='';
}
}
Sample usage
<input id="file" name="file" onchange="javascript:checkFileType(this.value);" type="file" />
function checkFileType(str){
file_name=str.replace(/^.*(\\|\/|\:)/, '');
ext=file_name.split('.').pop().toLowerCase();
if( ext!= 'doc' && ext!= 'jpg' && ext!= 'jpeg' && ext!= 'png' && ext!= 'gif' && ext!= 'pdf'){
alert("Selected file "+file_name+" is invalid, Please upload file of type doc,jpg,jpeg,pdf,gif or png. ");
document.getElementById('file').value='';
}
}
Sample usage
<input id="file" name="file" onchange="javascript:checkFileType(this.value);" type="file" />
Comments
Post a Comment