jQuery code to validate and stop sending

Mar 13, 2012   //   by phpfreelancer.biz   //   Blog, Jquery Tutorial  //  No Comments

I use this jQuery code to validate a file upload event. It’s successful. But When I click the submit button I don’t want that wrong file to be attached and passed to PHP.
How can I stop the file being sent to server if the file extension is wrong?
To fix this problem by: using event.preventDefault() on submit function

$('#form').submit(function(e) {

 var ext = $('#report_file').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['doc','docx','gif','jpg','png','xls','xlsx','jpeg','pdf']) == -1) {
    alert('invalid extension!');  

This will prevent the form to submit when invalid extension find

Leave a comment

Share This Post

RSS Wordpress News