0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

JavaScriptでファイルのサイズ、拡張子をチェックする

Last updated at Posted at 2024-06-21
<script>
document.addEventListener('DOMContentLoaded', function () {
  const allowExtensions = '.(jpeg|jpg|png|gif)$'; // 許可する拡張子
  const file01= document.getElementById('file01');
  file01.addEventListener('change', function () {
    if(file01.files[0].size > 2000000) {
        alert("ファイルサイズは2MB以内にして下さい");
    file01.value = "";
        return; // 処理を中断
    }
    if (!file01.files[0].name.match(allowExtensions)) { // 許可する拡張子以外の場合
        alert('拡張子が jpeg, jpg, png, bmp, gif 以外のファイルはアップロードできません。');
        file01.value = "";
        return; // 処理を中断
      }
  });

});
</script>
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?