you need to check each of them using the each function in jquery or the for loop.
$('#name, #email, #user, #address, #phone').each(function(){ if ($(this).val().length === 0){ $(this).addClass('warning'); } else { alert("form submitted"); } });
Here is the same example with a for
var formElements = $('#name, #foo'); for (i = 0 ; i < formElements.length ; i++) { if ( $(formElements[i]).val().length === 0 ) $(this).addClass('warning'); else { alert("form submitted"); } }
Jorge source share