Tuesday, 25 October 2011

Upload file extension restriction in jquery

Image upload field only accept following extension gif, png, jpg,jpeg.
<form action="" method="post" enctype="multipart/form-data">
Upload Image:<input type="file" name="image" id="image_id"  />
</form>
following code used to limit the upload file extension
<script type="text/javascript">
$(function() {
   $('#image_id').change( function() {
    var ext = $('#image_id').val().split('.').pop().toLowerCase();
    if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
      $('#image_id').val("");
        alert('Please Enter the files in the format jpeg,gif,png');
       
    }
   });
});
</script>

0 comments:

Post a Comment