I'm trying to check errors on the server side and this will bring me a number depending on the type of error, the problem is that when editing remove and put a data attribute to then check with the focus, I've already tried what is commented, but I can not make it work, when I use the data
$(document).on('keyup', '.albaran', function () {
var albaran = $(this).val();
var input = $(this, '.albaran');
$.ajax({
type: "POST",
url: '@Url.Action("AlbaranValidar", "Inicio")',
data: { 'Albaran': albaran },
success: function (data) {
//input.removeAttr('data-error');
//input.removeData('error', null)
//input.data('error', data);
//input.attr('data-error', data);
//input.removeData('error', null)
//input.data('error', data);
input.removeAttr('data-error');
input.attr('data-error', data);
},
error: function (r) {
alert("Error");
}
});
});
$(document).on('focus', '.albaran', function () {
console.log($(this).data('error'));
if ($(this).data('error') == 1) {
$(this).tooltip({ 'trigger': 'focus', 'title': 'El campo no puede estar vacio' });
}
});
the controller
public int ValidarAlbaran(string Albaran)
{
int AlbaranError = 0;
if (Albaran == "" || Albaran == null)
{
AlbaranError = 1;
}
else if (Albaran.Length > 10)
{
AlbaranError = 3;
}
else
{
try
{
Convert.ToInt64(Albaran);
AlbaranError = 0;
}
catch
{
AlbaranError = 2;
}
}
return AlbaranError;
}
I do a console.log but I'm still bringing the first data that I already have.
If the field has no error, it brings the data to 0 but if I edit it and the error is greater than accepted characters it brings 1 but I write and it changes in the sun but in the console.log it is still 0
Edition In response to Alvaro Montoro
$(document).on('focus', '.albaran', function () {
console.log($(this).attr('data-error').toString()); //con este me imprimo el numero
console.log($(this).attr('data-error')) // con este no;
if ($(this).attr('data-error').toString() == "1") {
console.log('entro al if del focus')
$(this).tooltip({ 'trigger': 'focus', 'title': 'El campo no puede estar vacio' });
}
});
does not enter the condition of the focus