Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to put validation on a file at the the time of selection in Jquery?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 129
    Comment on it

    Sometimes we need to apply validations on file-size and file-type of a file while uploading. We can check this by attaching a function to the "change" event.

    Examlpe:

    <input type="file" name="uploadFile" id="uploadFile" class="upload" title="Choose a file to upload"> 
    <span id="fileName_error"></span>
    
    
    <script>
    $('#uploadFile').bind('change', function() {
                  handleUpload();
              });
    
    function handleUpload(){
    
              if($('#uploadFile').val() !='')
              {
                  if($('#uploadFile').val().indexOf('.')>=0){
                      var ext = $('#uploadFile').val().split('.').pop().toLowerCase();
                      if($.inArray(ext, ['jpeg','jpg','png','gif','tiff']) != -1) {
    
                          var size = $('#uploadFile')[0].files[0].size/1024/1024;
                          if(size > 3)
                          {
                              $('#fileName_error').text('Please upload the file less than 3MB.');
                              return false;
                          }
                          $('#fileName_error').text('');
                          return true;
                      }
                  }
                  $('#fileName_error').text('Please upload the valid file(e.g jpeg,jpg,png,gif,tiff).');
                  return false;
              }
              else
              {
                  return true;
              }   
          }
    
    </script>
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: