JQuery validation before ajaxForm

I am trying to use the "beforeSubmit" parameter in the jQuery ajaxForm plugin to validate the data, however the form will be submitted even if there are invalid form fields. Where am I wrong? thank,

$(document).ready(function() { 
function validator(){ 
        $("#commentForm").validate();
}

    $('#commentForm').ajaxForm({ 
        dataType: 'json',
        url: "http://highlandfamilyeyecare.com/contactengine.php",
        beforeSubmit:  validator,
        success:        function(data) { 
            $('ul.form').fadeOut("slow");
            $('ul.form').html(data.formula).slideDown('slow');}
    });
});

And html:

<ul class="form">


    <li>    
        <form method="post" action="form.php" id="commentForm">

        <label class="white">Your Name</label>
        <input class="text-input required" type="text" name="name" /></li>

    <li>
        <label class="white">Email</label>
        <input class="text-input required email" type="text" name="email"/></li>

    <li>
        <li><input type='submit' value="Submit" />
        </form></li>

</ul>
+3
source share
3 answers

The function beforeSubmit()returns nothing.

Do you want to:

function validator() { 
  return $("#commentForm").validate();
}

Suppose that exists $('#commentForm").validate()and correctly returns true / false.

+6
source

In the documentation , the validation function should return falseto prevent the form from being submitted.

+3
source

, validate() , . - , .

    $('#commentForm').validate({
        errorClass: "invalidField",
        submitHandler: function() {
            $('#commentForm').ajaxSubmit(ajaxFormOptions);
        }
    });

ajaxFormOptions - .

+2

Source: https://habr.com/ru/post/1736013/


All Articles