Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created May 5, 2014 18:50
Show Gist options
  • Select an option

  • Save h2rd/575a954540443cb46271 to your computer and use it in GitHub Desktop.

Select an option

Save h2rd/575a954540443cb46271 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
$(function(){
$('input#submit').click(function(){
var file = $('input[type="file"]').val();
var exts = ['doc','docx','rtf','odt'];
// first check if file field has any value
if ( file ) {
// split file name at dot
var get_ext = file.split('.');
// reverse name to check extension
get_ext = get_ext.reverse();
// check file type is valid as given in 'exts' array
if ( $.inArray ( get_ext[0].toLowerCase(), exts ) > -1 ){
alert( 'Allowed extension!' );
} else {
alert( 'Invalid file!' );
}
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment