Js validates several input fields with the same name

advertisements

Ok i have multy fields with same name, and i want to check is all fields are not empty. My code works if i have only one input, but i have no idea how to do that with more inputs

<input class = "new_input" type=text name="name[]"/>
<input class = "new_input" type=text name="name[]"/>

function validation(){

    var x = document.forms["form"]["name"].value;
    if(x ==='')
    {
       $("#warning").html("Morate uneti vrednost!").css('color','red');
    return false;
    }
    else
    {
        return true;
    }

}

for example if enter only one field, validation will work, and i want to check all fields


You have tagged jquery, so I have given something which works in jquery http://jsfiddle.net/8uwo6fjz/1/

$("#validate").click(function(){

    var x = $("input[name='name[]']")
    $(x).each(function(key,val){
    if($(val).val().length<=0)
    {
       $("#warning").html("Morate uneti vrednost!").css('color','red');

    }

    });

});