LoginSignup
3
2

More than 3 years have passed since last update.

JavaScript ファイル拡張子チェック

Last updated at Posted at 2020-10-21

ライブラリを使わない拡張子チェック

allowExtension.js
  const allowExtensions = '.(jpeg|jpg|png|bmp|gif)$'; // 許可する拡張子
  const onChangePicture = (e) => {
    if (e.target.files[0]) { // ファイル存在チェック
      if (!e.target.files[0].name.match(allowExtensions)) { // 許可する拡張子以外の場合
        alert('拡張子が jpeg, jpg, png, bmp, gif 以外のファイルはアップロードできません。');
        return; // 処理を中断
      }
  }

参考ページ
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/match

3
2
0

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
3
2