When validating a form, when all the fields that are checked in selInputs () are filled, the alert pops up twice. Tell me what the problem is?

var newsForm = $('#add_news_form'), themesForm = $('#add_theme_form'); function selInputs(arg) { var title = arg.children('.add_mat_title').find('.text'), shortDesc = arg.children('.add_mat_short').find('.text'), text = arg.children('.add_mat_text').find('.text'), source = arg.children('.add_mat_source').find('.text'), tags = arg.children('.add_mat_tags').find('.text'), inputs = []; inputs.push(title, shortDesc, text, source, tags); for (var i = 0; i < inputs.length; i++) { if (inputs[i].val() == null || inputs[i].val() == '') { return false; } } return true; } function formValidation(form) { $(this).submit(function() { if (selInputs($(this)) === false) { alert('Заполните все поля!') return false; } }); } formValidation(newsForm); formValidation(themesForm); 

    1 answer 1

    So your function is called twice. And it turns out that if in both forms selInputs($(this)) == false, the alert also pops up twice. Try onsubmit="formValidation(this)" this form to the arguments of each form: onsubmit="formValidation(this)" and remove all the formValidation calls at the bottom of the script.