You can check for the extension ...
$('form').submit(function(event) { var file = $('input[type=file]').val(); if ( ! file) { alert('The file is required.'); event.preventDefault(); return; } if (file.match(/\.(?:csv|xl)$/)) { alert('CSV or Excel files only!'); event.preventDefault(); } });
... or you can check for the mime type.
$('form').submit(function(event) { var file = $('input[type=file]').prop('files')[0]; if ( ! file) { alert('The file is required.'); event.preventDefault(); return; } var mime = file.type; if (mime != 'text/csv' || mime != 'application/vnd.ms-excel') { alert('CSV or Excel files only!'); event.preventDefault(); } });
Of course, you also need to check on the server, this code is just a courtesy for users included in JavaScript.
Also choose something better than alert() . This is not the most convenient way to report an error.
source share