I have that code, I want to validate by means of the corresponding message, in what arrangement is the data entered, whether it is authorized or not. The data is entered, and the message appears according to the location of the data in the array, whether it is authorized or not. And that the other data appear to me also, as it is in the code.
<script>
var usuariosAutorizados = [
{"nombre": "88888888", "agente": "MARCK IBRAIMOVICH", "user": "P67312"},
{"nombre": "77777777", "agente": "Miguel de Cervantes Saavedra", "user":"L97634"}
];
var usuariosNoAutorizados = [
{"nombre": "99999999", "agente": "Elon Muss Bodytech", "user": "S35678"},
{"nombre": "66666666", "agente": "Pablo Villanueva Melcocha", "user": "G15948"}
];
function ajax_get_json(usuarioIngresado) {
var resultado = document.getElementById("info");
if (usuarioIngresado.length === 0) {
resultado.innerHTML = "";
} else
{
var datos = usuariosAutorizados;
var x = encontrarPersona(datos, usuarioIngresado);
// compruebas la longitud del array en lugar de true o false
var mensaje = (x.length > 0) ?
"<span class= 'autorizado'> Si está autorizado</span>" :
"<span class= 'noautorizado'>No está autorizado</span>";
for (var numUsuarios = 0; numUsuarios < x.length; numUsuarios++)
{
mensaje += "<span> <br> Usuario: " + x[numUsuarios].user +"<br>" +
"Agente: " + x[numUsuarios].agente + "<br>" +
"</span>";
}
resultado.innerHTML = mensaje
}
}
function encontrarPersona(objetoJSON, usuario) {
var arreglo = [];
for (var i in objetoJSON) {
var persona = objetoJSON[i];
// sólo agregamos al array si el nombre coincide (y agregamos todo)
if (persona.nombre == usuario) arreglo.push(persona);
}
// devolvemos el array
return arreglo;
}
</script>