I'm trying to validate an input using jquery validator 'addMethod'. The main goal is to check if the current user id exists in the system and if it does a error message is displayed. To check if the user exists I make a ajax call to my controller and it returns true or false.
$("input[name='VATNumber']").blur(function () {
var tbody = $(this).showLoading();
var vatnumber = $(this).val();
var svcUrl = "@Url.Action("CheckUserExistance", "User")";
$.ajax({
url: svcUrl + "?vatNumber=" + vatnumber,
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
async: true,
cache: true,
error: function (xhr, err) {
tbody.hideLoading();
$.jGrowl("ERROR checking user.", { life: 5000, group: "error" });
},
success: function (result) {
tbody.hideLoading();
displayErrorMessage(result.data);
//console.log(result);
//if (result.data == false) { displayErrorMessage(); }
}
});
function displayErrorMessage(result) {
console.log(result);
jQuery.validator.addMethod("checkUser", function (value) {
return result == false;
}, "User already exists.");
jQuery.validator.classRuleSettings.checkUser = { checkUser: true };
$("input[name='VATNumber']").validate();
}
});
The problem is that the validator is not displaying the message at the right time, even so the 'result' variable is right.
Can someone help, please?
UPDATE
I've tried with this code and it still doesn't work. The request is not considered.
var vatnumber = $("input[name = 'VATNumber']").val(),
officeid = parseInt($("input[name='OfficeID']").val(), 10);
var svcUrl = "@Url.Action("CheckUserExistance", "User")";
$('#mainForm').validate({
rules: {
VATNumber: {
required: true,
VATNumber: true,
remote: {
url: svcUrl,
type: "post",
data: {
vatNumber: vatnumber,
officeId: officeid
}
}
}
},
messages: {
VATNumber: {
remote: "User already exists."
}
}
});
I'm using ASP.NET MVC and Bootstrap
HTML
<div class="par control-group">
<label class="control-label" for="VATNumber">NIF</label>
<div class="controls">
<input class="input-large" data-val="true" data-val-maxlength="MaxLengthAttribute" data-val-maxlength-max="50" id="VATNumber" name="VATNumber" type="text" value="2345678">
<span class="field-validation-valid" data-valmsg-for="VATNumber" data-valmsg-replace="true"></span>
</div>
</div>
SCRIPT
$(document).ready(function () {
//$("input[name='VATNumber']").addClass("checkUser");
var vatnumber = $("input[name = 'VATNumber']").val(),
officeid = parseInt($("input[name='OfficeID']").val(), 10);
var svcUrl = "@Url.Action("CheckUserExistance", "User")";
console.log(svcUrl + " " + vatnumber);
$('#mainForm').validate({
rules: {
VATNumber: {
required: true,
remote: {
url: svcUrl,
type: "post",
// for troubleshooting
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log('status: ' + textStatus); // status
console.log('error: ' + errorThrown); // error
},
complete: function (jqXHR, textStatus) {
// fires after the request, even if there is an error
console.log('ajax is finished: ' + textStatus);
}
}
}
},
messages: {
VATNumber: {
remote: "User already exists."
}
}
});
});