I want to call a function only if the form is valid.
The check works correctly, but I cannot correctly call the addUser () function. addUser works, but it is looped, on the next call it works 2 times, then 5, and so on.
And another form should be sent only to addUser (). Individually, they work correctly, but not together.
There may be several phones.
---------- if (lastName.value.match(letters)) { for(var i = 0; i < phones.length; i++){ if (!phones[i].value.match(digts)) { error.innerHTML = 'Only digits'; frm.insertBefore(error, phones[i]); errorMessage = "false"; phones[i].focus(); if(phones[phones.length - 1].value != '') { addUser(); }; break; } } } else { if(user.value == ''){ //code } errorMessage = "false"; } if (errorMessage !== "") { event.preventDefault(); } } ---------- function addUser(){ $('#registration').submit(function(event) { alert(12); //code event.preventDefault(); var data = 'phones=' + JSON.stringify(arrUserInfo); $.ajax({ //code }); }); }
addUsercall adds asubmithandler - Grundy$('#registration').submit(function(event) {...})hangs up the handler, but does not execute the function. This means that you should not call theaddUsermethod several times. At least in its current form. - GrundyaddUserin a loop, are these handlers hung up? Do I need to remove the check foraddUserto another place? but I tried and doesn’t help @Grundy - Misha Podlevsky$('#registration').submit(). But I still have little idea how the provided code is related and theaddUserfunctionaddUserso maybe you need to do something else - Grundy