Check a file before uploading

Reject big or wrong files in the browser first. The server must still check again.

jQuery · Check a file before uploadingIntermediate
const MAX_MB = 2;
const ALLOWED = ['image/jpeg', 'image/png', 'image/webp'];

$('#photo').on('change', function () {
  const file = this.files[0];
  let error = '';

  if (file) {
    if (ALLOWED.indexOf(file.type) === -1) error = 'Only JPG, PNG or WebP images';
    else if (file.size > MAX_MB * 1024 * 1024) error = 'Maximum size is ' + MAX_MB + ' MB';
  }
  $('#photo-error').text(error);
  if (error) $(this).val('');                       // clear the choice
});
file inputvalidationsizetype