A
A
Atlanta2018-11-23 21:48:51
Layout
Atlanta, 2018-11-23 21:48:51

I'm building a website on a resume, how can I prevent the form from reloading the page (code inside)?

There are several forms on the page, they are validated by the jQuery plugin. And if the validation is successful, I need the form fields to be cleared and a message appears that we will call you back. But as soon as the validation passes, the page is successfully updated.

<form class="form js-form" id="form-request" role="form">
    <input class="name" placeholder="Full name" type="text" name="name">
    <input class="email" placeholder="Email Address" type="email" name="email">
    <input class="phone" placeholder="Phone Number" type="tel" name="tel">
    <button type="submit" class="btn">request a quote</button>
</form>

The each method is used because I have multiple forms being validated.
$('.js-form').each(function () {
        $(this).validate({
            rules: {
                email: {
                    required: true,
                    email: true
                },
                name: {
                    required: true,
                    minlength: 2
                },
                tel: {
                    required: true,
                    digits: true,
                    rangelength: [5, 11]
                },
                submitHandler: function () {
                    alert('почему он не срабатывает? и как очистить поля)?');
                    closeModal();
                }
            }
        });
    });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Atlanta, 2018-11-23
@Atlanta_Boston

In general, the mistake was stupid. I wrote the submitHandler property in rules.
corrected code)

$('.js-form').each(function () {
        $(this).validate({
            rules: {
                email: {
                    required: true,
                    email: true
                },
                name: {
                    required: true,
                    minlength: 2
                },
                tel: {
                    required: true,
                    digits: true,
                    rangelength: [5, 11]
                }
            },
  ----->     submitHandler: function () {
                alert('почему он не срабатывает? и как отчистить поля)?');
                closeModal();

            }
        })
    })

V
Vladimir, 2018-11-23
@Casufi

It's all about the button
element .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question