Categories
JavaScript

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:

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