Created
August 23, 2023 12:38
-
-
Save netojocelino/b3e1b70d84694a8669125f0e8e5d596b to your computer and use it in GitHub Desktop.
Arquivo informar que existem limitantes de tamanho em inputs de arquivo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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