I have a small form, the fields of which I check. I need to organize a check if the user fills in the first two inputs with the text type and one input with the file type, the data is sent, otherwise an error, and this option, if the user does not select the file, then he must fill in all 11 fields with the text type, otherwise mistake. Tell me what am I doing wrong?
$("form").eq($indx_form).on("click", $el, function (event) { event.preventDefault(); var data = new FormData(document.forms[1]); data.append( 'file', $('input[type=file]')[0].files[0] ); var cnt=0; var first_three= $("#conclusion-treaty input:lt(2)"); $('#conclusion-treaty input[type="text"]').each(function(i){ val=$(this).val(); if(val.length!=0 || val.length!="" || val.length!="undefined") cnt++ console.log(cnt,i) }) if( (cnt != 11 ) || ( (first_three.length!=2) || ( $("input[type=file]").val()=="") ) ){ $(".info_block").html("Вы не заполнили поля"); console.log(cnt) } else { $(".info_block").html("Ваше сообщение отправлено"); $.ajax({ type: "POST", processData: false, contentType: false, url: "read.php", data: {data}, success: function(){ alert('Ваши данные оправленны'); } }) .done(function( file ) { console.log(file) }); } }); input{ display:block; margin:10px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <form action=""> <input type="text" name="item1"> <input type="text" name="item2"> <input type="file" name="file"> <input type="text" name="item3"> <input type="text" name="item4"> <input type="text" name="item5"> <input type="text" name="item6"> <input type="text" name="item7"> <input type="text" name="item8"> <input type="text" name="item9"> <input type="text" name="item10"> <input type="text" name="item11"> <input type="submit" id="send"> </form>
$("form").on("submit",function(e){})- alexoander