Click here to Skip to main content
15,876,879 members

Comments by MiaPe (Top 2 by date)

MiaPe 16-Jun-21 5:48am View    
Ok, I try to explain myself better.
I have some text type inputs in a form.
With a single function, I would like Javascript to check the length of each input.
If the length is less than 3, the function should give an error message. Now the error message is not displayed. Why?
I hope I have been clearer.
Thank you
MiaPe 16-Jun-21 3:21am View    
No working.


(function() {
const selectionCheckbos = document.querySelector('.section');
const text = document.querySelectorAll('input[type=text]');
const textLength = text.length;


function initText() {

for (let i = 0; i < textLength; i++) {
text[i].addEventListener('change', textValidity);
}

textValidity();
}


function chekText() {
const min = 3;
for(let i = 0; i < textLength; i++) {
if (text[i].length < min) {
return false;
}
return true;
}
}


function textValidity() {
const errorMessage = !chekText() ? 'Il campo non valido' : '';
text.setCustomValidity(errorMessage);
}


})();