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

Related Posts Related Websites
Help sharing and Flatter me ;)

Share

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