April 18, 2012
jQuery validation – How to not accept a file extension
Question by Maxime Lepers
I would like to find a way to refuse a specific extension in a text field at form submission.
I have a field where you can specify a URL but this URL shouldn’t link to a PDF file.
I figured that there is a jQuery validation methods called accept
that does exactly the contrary of what I want to do.
Is there a way to use with a not()
function or something similar? It would be way easier than creating a custom validation method.
Answer by Starx
Here is any idea
var ext = $('#fieldid').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['gif','jpg', ...]) == -1) {
alert('invalid extension!');
}