Reputation: 562
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.
Upvotes: 1
Views: 1480
Reputation: 1
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.");
Upvotes: 0
Reputation: 562
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();
Upvotes: 1