0

Way To Validate Using For Loop?

I'm trying to figure out if there's a way to validate something using a for loop instead of an if that checks everything. https://code.sololearn.com/WKQkcv451d4c/?ref=app

25th Jul 2017, 1:47 AM
MemeSenpai
MemeSenpai - avatar
2 ответов
+ 4
How about something this? Or you can add class name for input elements that you want to validate then use "getElementsByClassName" instead of "getElementsByTagName". var i=document.getElementsByTagName("input"); for (x=0; x<i.length; x++){ if (i[x].value === ""){ alert("Enter Something In All Fields!"); return false; } } return true;
25th Jul 2017, 3:56 AM
Bàng Tứ Cường
Bàng Tứ Cường - avatar
+ 3
If you want to loop it, you could have everything you want in an array. Then loop through the array, if any field is not filled, let them know and return the function accordingly. Perhaps like this: var elements = ["Fullname", "Username", "Password", "Email"]; for(var i = 0; i < elements.length; i++) if (document.getElementById(elements[i]).value == ""){ alert("Enter Something In All Fields!"); return false; } return true;
25th Jul 2017, 3:58 AM
Rrestoring faith
Rrestoring faith - avatar