jQuery validate: call a function after form validate

10.5k Views Asked by At

I'm trying to call a function that will save my form.

Here's my code:

$('#save-form-datas').validate({
    rules: {
        ROO_Number: {
            required: true,
            minlength: 1,
            maxlength: 4
        }
    },
    submitHandler: function(form) {
        var formData = new FormData(form);
        saveFormDatas(form);
    }
});

But when the code is executed it doesn't work.

Here's the code of the function I want to execute right after the for has been validate:

function saveFormDatas(form) {

    $.ajax({
        type : 'POST',
        data : form.serialize(),
        url  : 'assets/app/php/ajax/rooms_edit.php',
        success: function(responseText){
            var json = $.parseJSON(responseText);
            if(json.type=="success") {
                $('#status-message-saved.hide').removeClass('hide');

                if(json.return_url!="") { 
                    setTimeout(function(){ window.location.href = "app?q=" + json.return_url; }, 1000);
                }
            }
            if(json.type=="error") {
                $('#status-message-error.hide').removeClass('hide');
            }
        }
    });
}

But it doesn't work and simply open a new page like this I've passed the form with $_GET (all my form datas are in the url).

I need to make like this because I have more than 100 forms and can't duplicate my code to save the forms.

3

There are 3 best solutions below

0
On BEST ANSWER

Just remove the submitHandler from the validator and add a external function.

$('#save-form-datas').validate({
 rules: {
    ROO_Number: {
        required: true,
        minlength: 1,
        maxlength: 4
    }
 }
});

Handle form submit as below.

$('#save-form-datas').submit(function(event){
    event.preventDefault();
    if($('#save-form-datas').valid()){
        var form = $('#save-form-datas')[0];
        var formData = new FormData(form);
        saveFormDatas(form);
    }
});

Or else you can just add "return false" as below...(This will prevent the default form submit)

$('#save-form-datas').validate({
 rules: {
    ROO_Number: {
        required: true,
        minlength: 1,
        maxlength: 4
    }
 },
 submitHandler: function(form) {
    var formData = new FormData(form);
    saveFormDatas(form);
    return false;
 }
});
0
On

jQuery Validate exposes an option parameter called showErrors, which receives a map and a list of errors. All you have to do is bind there:

$("#myform").validate({
  showErrors: function(map,errors) {
    var validator = this;
    onValidate(validator);
    return validator.defaultShowErrors(map,errors);
  }
});

function onValidate(validator) {
  var isValid = validator.valid();
  // do something ...
}

Also, you can force a validation once when the Javascript is loading, for init and buttons and stuff: $("#myform").data('validator').form();

0
On

I had done with this in my code

$('#save-form-datas').validate({
    rules: {
        ROO_Number: {
            required: true,
            minlength: 1,
            maxlength: 4
        }
    }
});

here you can check if form valid or not

if($('#save-form-datas').valid()){
      function myfunction()
}

myfunction {

$.ajax({
        type : 'POST',
        data : $('#save-form-datas').serialize(),
        url  : 'assets/app/php/ajax/rooms_edit.php',
        success: function(responseText){
            var json = $.parseJSON(responseText);
            if(json.type=="success") {
                $('#status-message-saved.hide').removeClass('hide');

                if(json.return_url!="") { 
                    setTimeout(function(){ window.location.href = "app?q=" + json.return_url; }, 1000);
                }
            }
            if(json.type=="error") {
                $('#status-message-error.hide').removeClass('hide');
            }
        }
    });
}

Hope This Helps.