Skip to content

Instantly share code, notes, and snippets.

@netojocelino
Created August 23, 2023 12:38
Show Gist options
  • Select an option

  • Save netojocelino/b3e1b70d84694a8669125f0e8e5d596b to your computer and use it in GitHub Desktop.

Select an option

Save netojocelino/b3e1b70d84694a8669125f0e8e5d596b to your computer and use it in GitHub Desktop.
Arquivo informar que existem limitantes de tamanho em inputs de arquivo
Array.from(document.querySelectorAll('input[type=file][data-max-size]')).forEach(el => {
el.addEventListener('input', function () {
const maxSize = this.dataset.maxSize;
if (maxSize === undefined) return;
let isValid = true;
for (let i = 0; i < this.files.length; i++) {
const file = this.files[i]
if (file.size >= maxSize) {
isValid = false;
const mbSize = maxSize / (1024 * 1024);
console.error(`Tamanho não pode ser maior que ${mbSize}Mb`)
if (!this.parentNode.querySelector('span.invalid-feedback')) {
const span = document.createElement('span')
const text = document.createTextNode(`Tamanho não pode ser maior que ${mbSize}Mb`)
const attr = document.createAttribute('class')
attr.value = 'invalid-feedback'
span.setAttributeNode(attr)
span.appendChild(text)
this.insertAdjacentElement('afterend', span)
}
this.classList.add('is-invalid')
return
}
}
if (isValid) {
this.classList.remove('is-invalid')
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment