S
S
SNeverov2022-03-13 18:21:53
AJAX
SNeverov, 2022-03-13 18:21:53

How not to submit an empty form?

Help me please!
I'm learning from a video course. The essence of the question is this. The site has a feedback form. The fact is that the forms are sent even if the fields are not filled. How can I prevent forms from submitting if fields are empty? As I understand it, it is necessary to write somewhere and somehow correctly in JS that if the form does not pass validation, then it will not be sent. But I’m still far from learning JS, but I want to deal with the problem now. Sending mail is implemented through PHPMailer. Here is a link to the site https://pulse.neverov.site/

Here is the html code of the forms (modal windows)

<div class="overlay">
        <div class="modal" id="consultation">
            <div class="modal__close">&times;</div>
            <div class="modal__subtitle">Просто заполните форму заявки</div>
            <div class="modal__descr">и мы перезвоним вам в течении 10 минут</div>
            <form class="feed-form feed-form_mt25" action="#">
                <input name="name" placeholder="Ваше имя" type="text">
                <input name="phone" placeholder="Ваш телефон">
                <input name="email" placeholder="Ваше E-mail" type="email">
                <button class="button button_submit">заказать консультацию</button>
            </form>
        </div>

        <div class="modal" id="order">
            <div class="modal__close">&times;</div>
            <div class="modal__subtitle">Ваш заказ:</div>
            <div class="modal__descr">Пульсометр Polar FT1</div>
            <form class="feed-form feed-form_mt25" action="#">
                <input name="name" required placeholder="Ваше имя" type="text">
                <input name="phone" required placeholder="Ваш телефон">
                <input name="email" required placeholder="Ваше E-mail" type="email">
                <button class="button button_submit">купить</button>
            </form>
        </div>

        <div class="modal modal_mini" id="thanks">
            <div class="modal__close">&times;</div>
            <div class="modal__subtitle">Спасибо за вашу заявку!</div>
            <div class="modal__descr">Наш менеджер свяжется с вами в ближайшее время!</div>
        </div>


Here is the modal windows JS script
$('[data-modal=consultation]').on('click', function () {
        $('.overlay, #consultation').fadeIn('slow');
    });
    $('.modal__close').on('click', function() {
        $('.overlay, #consultation, #thanks, #order').fadeOut('slow');
    });

    $('.button_mini').each(function(i) {
        $(this).on('click', function() {
            $('#order .modal__descr').text($('.catalog-item__subtitle').eq(i).text());
            $('.overlay, #order').fadeIn('slow');
        })
    });

    function valideForms(form){
        $(form).validate({
            rules: {
                name: {
                    required: true,
                    minlength: 2
                },
                phone: "required",
                email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                name: {
                    required: "Пожалуйста, введите своё имя",
                    minlength: jQuery.validator.format("Введите не менее {0} символов!")
                },
                phone: "Пожалуйста, введите свой номер телефона",
                email: {
                  required: "Пожалуйста, введите свою почту",
                  email: "Неправильно введён адрес почты"
                }
            }
        });
    };

    valideForms('#consultation-form');
    valideForms('#consultation form');
    valideForms('#order form');

    $('input[name=phone]').mask("+7 (999) 999-99-99");

    $('form').submit(function(e){
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "mailer/smart.php",
            data: $(this).serialize()
        }).done(function() {
            $(this).find("input").val("");
            $('#consultation, #order').fadeOut();
            $('.overlay, #thanks').fadeIn('slow');

            $('form').trigger('reset');
        });
        return false;
    });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
irishmann, 2022-03-14
@irishmann

Set the required attribute for the required fields. Until you fill them in, submit will not work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question