0

I am validating form using jquery . All is working fine but not file uploading. here there is a file upload button which accept only images for that I use Jquery as follow,

$(document).ready(function(){


    $('#create_teacher').validate({
    rules: {
        teacherId: {

        required: true
      },

        teacherName: {
        minlength: 6,
        required: true
      },

        education: {
            required: true,
            minlength: 6
        },

        experience: {
            required: true,
            minlength: 6

        },

        prevdetails:{
            required: true,
            minlength: 6

        },
        email: {
        required: true,
        email: true
      },


        highlight: function(element) {
            $(element).closest('.control-group').removeClass('success').addClass('error');
        },
        success: function(element) {
            element
            .text('OK!').addClass('valid')
            .closest('.control-group').removeClass('error').addClass('success');
        }
    }
  });


$('#create_teacher input[type="submit"]').click(function(e){
        e.preventDefault();
        var form = $('#create_teacher');
        var file = $('input[type="file"]', form).val();
        var exts = ['jpg','jpeg','gif','png'];
        var msg = $('.msg', form);
        msg.hide();

        // 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 ){
                msg.show().html( '<strong style="color:#090">Allowed extension!</strong>' );
            } else {
                msg.show().html( '<strong style="color:#f00">Invalid file!</strong>' );
            }
        }else{
            msg.show().html( '<strong style="color:#f00">Select file!</strong>' );
        }
    });

}); // end document.ready

and my file picker as follow,

<input type="file"  id="photo" name="photo" />

but this is not validating . Please help me.

Raghavendra
  • 259
  • 4
  • 12
  • 23

2 Answers2

0

Make Sure your form looks like this :

<form action="#" method="post" " enctype="multipart/form-data" class="" onsubmit="return Checkfiles(this);">
// ...
  <input type="file" name="file" id="file">
</form>

Jquery :

  function Checkfiles(f){
      f = f.elements;
      if(/.*\.(jpg)$/.test(f['file'].value.toLowerCase()))
        return true;
      alert('Please Upload Jpg Files Only.');
       f['file'].focus();
       return false;
};
Gtopuria
  • 429
  • 2
  • 8
  • 25
0

jQuery Validate actually supports this directly, via additional-methods.js, using the accept rule.

What you would do is include that .js file in your page, and add a rule to your rules object that looks like this:

photo:{
    accept:'image/*'
}

Which will allow all image mimetypes.

Ryley
  • 21,046
  • 2
  • 67
  • 81