The function ValidateForm
doesn't have a return
statement. The only one you have is in the anonymous function you pass to forEach
- Define a variable before you call
forEach
- Give it a default value (probably
true
) - Change it as you go around the loop (the specific logic will depend on what you are looking for, but will probably be
if (condition) { retVal = false; }
with noelse
) - Return it at the end
Such:
var retVal = true;
formelements.forEach(function(obj) {
if(obj.value=="") {
obj.style.borderColor="#FF0000";
alert(obj);
retVal = false;
});
return retVal;
Alternatively, use a traditional for loop:
for (var i = 0; i < formelements.length; i++) {
var obj = formelements[i];
if (obj.value == "") {
obj.style.borderColor="#FF0000";
alert(obj);
return false;
}
}
Although this approach will stop as soon as a single failure is found.