Home  »     »  JS function to check the extension of file in the url

JS function to check the extension of file in the url

Wednesday, April 27, 2011

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" />




How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
JS function to check the extension of file in the url.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »