Check file extension in JavaScript

Easy script to check extensions on JavaScript.

function checkExt(e) {//use in a form event or ina input
       value=e.value;
	if( !value.match(/\.(jpg)|(gif)|(png)|(bmp)|(pdf)$/) ){//here your extensions
		alert("wrong extension");	//actions like focus, not validate...
	}
	else {//right extension
		alert("nice!");	//actions
	}
}

Usage example onchange event:

<input id="Path" name="Path" type="file" />

Just one that says if is PDF or not:

function isPdf(e) {
if( e.value.match(/\.(pdf)$/) ){
	alert("pdf");	//action
      }
	else alert("no pdf");	//actions
}

Same as the other one.

I know they are really simple but I never need it to validate this in the client side since today. Kind of weird xD

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • FriendFeed
  • Meneame
  • Netvibes
  • Reddit
  • StumbleUpon
  • Tumblr
  • Wikio
  • RSS
  • email
  • PDF
  • Print

Related posts:

  1. Detect file extension in PHP
  2. Check requirements for PHP web application
  3. Concatenate PDF in PHP
  4. isArray JavaScript
  5. Confirm before click in JavaScript

2 Comments

  1. Ramachandran says:

    Great! it worked perfectly!, Thanks for your code. After a very long hunt i found this.

    Thanks
    Ram

Leave a Reply

Follow me