I
I
It2019-12-03 15:21:25
JavaScript
It, 2019-12-03 15:21:25

How to submit data only after HTML5 form validation?

There is a form on the site, data is sent via ajax to the server with node js.

<form action="/page-mail" id="email-form" class="form" method="post" onSubmit="return false;">
          <input type="text" id="form__fname" name="first_name" placeholder="First Name ">
          <input type="text" id="form__lname" name="last_name" placeholder="Last Name ">
          <input type="email" class="form__email" name="email" placeholder="Business email *" required>
          <input type="phone" class="form__phone" name="phone" placeholder="Phone number *" required>
          <p class="error-msg"></p>
          <button type="submit" class="form__submit" id='sendButton' >Download now</button>
        </form>

$(document).ready(function() {
      $("#email-form").submit(function(e) {
        var action = $(this).attr("action");
        var ser = $(this).serialize();
        var that = this;
        $.ajax({
            type : "POST",
            url : action,
            data : ser,
            complete : function(data) {
                if (data.responseText == "true") {
                    ga('send', 'event', { eventCategory: '', eventAction: ''});
                    window.location.replace('/page-thank-you');
                } else {
                    $(".error-msg").html('<div class="alert-box alert">' + data.responseText + '</div>');
                    $("#sendButton").attr('disabled', false);
                }
            }
        });
        return false;
      });
    });

Now data is sent in any scenario without validation, despite the required inputs (and redirects to than you page). How to fasten the standard validation check? So that when you click on the submit button, standard errors appear first, and sending after the validation is passed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2019-12-03
@Casufi

Strange, the validation works without problems
https://jsfiddle.net/azh2L6x0/1/
Are you sure you have an HTML5 page?
htmlbook.ru/html/%21doctype

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question