How to make a check on the conditions of registration?

setInterval(function() { var $first_pass = $('.first_pass').val(); var $second_pass = $('.second_pass').val(); var $login_val = $('.login_input').val(); if ($first_pass.length === 0) { $('.ok_pass').hide(); $('.not_ok_pass').hide(); $('.form_reg').attr('action', '#'); } else if ($first_pass !== $second_pass) { $('.ok_pass').hide(); $('.not_ok_pass').fadeIn(); $('.form_reg').attr('action', '#'); } else if ($first_pass == $second_pass) { $('.ok_pass').fadeIn(); $('.not_ok_pass').hide(); $('.form_reg').attr('action', 'reg_action.php'); }; $.ajax({ url: 'check.php', type: "POST", data: $('.form_reg').serialize(), success: function(check) { var $check_login = check; if ($check_login == 1) { $('.ok').fadeIn(); $('.not_ok').hide(); $('.not_ok_login').fadeOut(); $('.form_reg').attr('action', 'reg_action.php'); } else if ($check_login == 0) { $('.not_ok_login').fadeOut(); $('.ok').hide(); $('.not_ok').fadeIn(); $('.form_reg').attr('action', '#'); } else if ($check_login == 2) { $('.ok').fadeOut(); $('.not_ok').fadeOut(); $('.form_reg').attr('action', '#'); } } },1000); if (!$('.first_pass').val() && !$('.second_pass').val() && !$('.login_input').val()) { $('.form_reg').attr('action', '#'); }; }); <div class="container"> <div class="login_holder"> <img src="../img/logo.png" id="test" alt="" style="margin: 0 auto; display: block;"> <form class="form_reg" action="#" method="POST"> <input type="text" class="login_input" name="login" placeholder="Логин"> <p class="not_ok">Простите, этот логин уже занят.</p> <p class="ok">Этот логин можно использовать!</p> <p class="more_3">Логин должен быть больше 3 символов</p> <input type="text" class="login_input" name="email" placeholder="Email"> <input type="password" class="login_input first_pass" name="password" placeholder="Пароль"> <p class="not_ok_pass">Пароли не совпадают!</p> <p class="ok_pass">Пароли совпадают!</p> <input type="password" class="login_input second_pass" placeholder="Повторите пароль"> <input type="submit" class="submit_input" value="Зарегестрироваться"> </form> </div> 

Closed due to the fact that off-topic participants are Alexey Shimansky , Denis Bubnov , Vadim Ovchinnikov , fori1ton , aleksandr barakin Jan 27 '17 at 15:46 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Alexey Shimansky, Denis Bubnov, Vadim Ovchinnikov, fori1ton, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    To check through the interval the correctness of the input is pretty cruel.
    All fields have an event change ( article / lesson on working with events for fields ), which pops up when a person does any field manipulations. Use a ready-made solution http://rickharrison.imtqy.com/validate.js/ for example.

    There are quite a few validators on the Internet, but this one, in my opinion, is the most convenient and working.
    I would advise you to use native check from browsers, but safari still does not support it.

    The validator first checks the form, then allows it to be sent; when it allows it to be sent, you interrupt the sending of the native with event.preventDefault(); , and then collect data from the form and send via ajax.

    If you need a backend form check, then do it through one script, and not through several. You should have one script responsible for these simple things, not a few.

    Also, do not create multiple blocks with "errors" and "everything is ok." You have one unit that can be responsible for this, just depending on the situation, change its content and class which colors it in red or green.

    If you use jQuery and ajax sending, then in the form attributes you can not write anything at all. And use the event submit

     $('form').on('submit', function(e) { e.preventDefault(); console.log($(this).serialize()); }) 

    https://jsfiddle.net/dimensi/8Lzoqw7s/

    • Thank you very much! - Sergey Dymov