1

I don't want to refresh page after submitting form, so i used form.preventDefault(); to not do it. I want to validate form and then (if it is valid), i want to call backend function (via ajax) to insert a new row to database.

Everything works well for the first submit but for the second validation it doesn't fire remote validation so when i am trying to add next SN which is the same as the last one, i am able to do that. I can't understand why remote validation is not fired while others validation (like min characters/max characters) are fired without any problems.

Code:

$("#form").validate({
    onkeyup: false,
    onfocusout: false,
    rules: {
        sn: {
            required: true,
            minlength: 8,
            maxlength: 8,
            remote: {
                url: "ajax.php",
                type: "get",
                data: {
                    class: "board",
                    action: "IsFreeSN"
                }
            }
        }
    },
    messages: {
        sn: {
            required: "SN is required",
            minlength: "SN has to be 8 chars long",
            maxlength: "SN has to be 8 chars long",
            remote: "SN is already in database"
        }
    },
    submitHandler: function(form) {
        // Form is valid, so i am calling backend function for database insert
        form.preventDefault(); // preventing page reload
    }
});

As i said, when you run it for the first time (first submit), it works. But when you want to do it again (without page reload of course), it goes without remote validation.

5
  • Is the second SN-field added dynamically? Then you have to use rules('add') method
    – alexP
    Apr 2, 2014 at 9:14
  • Nope, SN field is simple HMTL input which exists since the first page load.
    – Manny
    Apr 2, 2014 at 9:15
  • Is $("#form").validate({ called once or on every submit?
    – alexP
    Apr 2, 2014 at 9:18
  • How do i find it? If it helps, all validation block is that code.
    – Manny
    Apr 2, 2014 at 9:27
  • The jquery-validation-engine is a totally different plugin than the one you show in your code. Please be more careful when tagging. You don't need any .preventDefault() in the submitHandler callback, and AFAIK, form.preventDefault() is invalid code, since form is not an event. As long as you use the submitHandler, the default submit action (and page reload) is always going to be blocked.
    – Sparky
    Apr 2, 2014 at 17:27

2 Answers 2

1

I did it. jQuery Validator does not fire remote validation if there is the same value as last one. It is reset on page refresh or through reset function. Solution is to use:

var validator = $("#form").validate({ ... });

and after successful validation call:

validator.resetForm();
0

I did use this.

jQuery.validator.addMethod("idTurnoEstaEnUso", function (value, element)
{        
  var url = '/nonrest/Turnos/IdTurnoValida';
  var objetoIdTurno = { inputTextIdTurno: value };
  var respuesta = null;

  $.ajax({     
    async: false,
    url: url,
    type: 'post',
    contentType: 'application/json',
    data: ko.toJSON(objetoIdTurno),
    success: function (data) {
      if (data == true) {
        respuesta = true;
      } else {
        respuesta = false;
      }
    },
    error: function () {
      respuesta = false;
    }
  });

  if (respuesta == false) {
    return this.optional(element) || respuesta;
  }

  if (respuesta == true) {
    return this.optional(element) || respuesta;
  }

}, "El código está ya en uso.");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.