Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Có thể validate kích thước và loại input=file trong HTML5 không?


Có, có thể validate kích thước và loại input type = “file”. Dùng jQuery để đạt kết quả −

<!DOCTYPE html> <html> <head>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head>    <body>       <form >          <input type="file" id="myfile" data-max-size="32154" />          <input type="submit"/>       </form>       <script>          $(function(){             $('form').submit(function(){                var val = true;                   $('input[type=file][data-max-size]').each(function(){                      if(typeof this.files[0] !== 'undefined'){                         var max = parseInt($(this).attr('max-size'),10),                         mySize = this.files[0].size;                         val = max > mySize;                         return val;                      }                   });                   return val;                });             });       </script>    </body> </html>