jQuery validation before AJAX submit

↘锁芯ラ 提交于 2019-12-22 12:39:21

问题


I have a simple signup form with the following jQuery validation code:

$(document).ready(function(){

    $("#registerForm").validate({

    rules: {

    Username: {required: true, minlength: 6},   
    Password: {required: true, minlength: 6},
    re_Password: {required: true, equalTo: "#Password"} 

    },

    });     
});

It validates correctly before I submit the form.

The problem comes when I want to also do an AJAX submit of the form, because the form no longer validates and submits without validation:

<form id="registerForm" action="register.php" method="post" onsubmit="xmlhttpPost('register.php', 'registerForm', 'signup-box'); return false;">

The default that works is:

<form method="post" id="registerForm" action="register.php">

Please, if anyone can point me in the right direction or give me some starting points to solve this I would be grateful.

Thanks.


Thanks to Kundan Singh Chouhan I have found the solution adding the following code to the document.ready block:

$("#registerForm").submit(function(){

    if($("#registerForm").valid()){

        xmlhttpPost('register.php', 'registerForm', 'signup-box'); 

   }

return false;

});

回答1:


I think you should have to remove the onsubmit event from your form tag and write the below script in document.ready()

$("form").submit(function(){
   if($("form").validate()){
      xmlhttpPost('register.php', 'registerForm', 'signup-box'); 
   }
   return false;
});

Hope this will fix your problem.




回答2:


I think you should use Submit handler for JQuery Validator, like so:

$('#id_of_form').validate(){

    // rules and messages

    submitHandler: function(form) {

    // place your code for Ajax Request here...

    }
}



回答3:


You could use jQuery.Validation here is some sample code:

    var $form = $("form");
    $form.validate({
        rules: {
            Username: {required: true, minlength: 6},   
            Password: {required: true, minlength: 6},
            re_Password: {required: true, equalTo: "#Password"} 
        },
        invalidHandler: function(event, validator) {
            // 'this' refers to the form
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors === 1
                  ? 'You missed 1 field. It has been highlighted'
                  : 'You missed ' + errors + ' fields. They have been highlighted';  
            }
        },
        submitHandler: function (form) {

            var $form = $(form);
            $.ajax({
                url: destinationUrl,
                method: "POST",
                data: $form.serialize()
                })
                .done(function (result) {
                    // show some message, etc...

                    return false; // blocks redirect after submission via ajax
                })
                .fail(function (response, error) {
                    // failed
                })
                .always(function() {

                });
        }
    });

regards.



来源:https://stackoverflow.com/questions/11703955/jquery-validation-before-ajax-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!