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 }); }); } 
  • That's all because every addUser call adds a submit handler - Grundy
  • And how to fix it? Could you tell? I did not understand how he adds. @Grundy - Misha Podlevsky 5:27 pm
  • calling $('#registration').submit(function(event) {...}) hangs up the handler, but does not execute the function. This means that you should not call the addUser method several times. At least in its current form. - Grundy
  • that is, because addUser in a loop, are these handlers hung up? Do I need to remove the check for addUser to another place? but I tried and doesn’t help @Grundy - Misha Podlevsky
  • you can try replacing the addUser call with $('#registration').submit() . But I still have little idea how the provided code is related and the addUser function addUser so maybe you need to do something else - Grundy

0