JSFiddle - React, Tailwind, and code Playground

by Brunno Romero

HTML

<span>VALORES de 1 a 3</span>
<BR/>
<input type="text" class="teste">
<BR/>
<input type="text" class="teste">
<BR/>
<input type="text" class="teste">
<BR/>
<BR/>
<BR/>
<span>VALORES de 1 a 5</span>
<BR/>
<input type="text" class="teste1">
<BR/>
<input type="text" class="teste1">
<BR/>
<input type="text" class="teste1">
<BR/>
<input type="text" class="teste1">
<BR/>
<input type="text" class="teste1">
<BR/>
<BR/>
<div class="resultado"></div>

JavaScript

var valorAnterior = 0; /* variavel global */
var valorAtual = 0;
var array1 = [];
var array2 = [];


$(document).ready(function () {
    verificaCamposRepetidos(".teste", array1);
    verificaCamposRepetidos(".teste1", array2);
});

function verificaCamposRepetidos(element, array) {

    //totalPerguntas = $(element).length;

    $(element).on("focus", function () {
        if ($(this).val() != "") {
            valorAnterior = $(this).val();
        }
    });

    $(element).on("change", function () {
        valorAtual = $(this).val();
        totalPerguntas = $(element).length;

        if (valorAtual > totalPerguntas || valorAtual <= 0) {
            if (valorAtual != "") {
                alert("Valor Digitado : " + valorAtual + "\nDigite Números de 1 a " + totalPerguntas);
                $(this).val("");
                return false;
            }
        }

        var index = array.indexOf(valorAnterior);

        if (index > -1) {
            array.splice(index, 1); // remove
        }

        var valor = $(this);
        verificaValoresRepetidos(valorAtual, valor, array);

    });

}

function verificaValoresRepetidos(valorAtual, valor, array) {

    if (valorAtual != "") {
        for (var j = 0; j < array.length; j++) {
            if (valorAtual === array[j]) {

                alert("Valor Digitado: " + valorAtual + "\nValor Repetido!!");
                valor.val("");
                return false;
            }
        }
        array.push(valor.val());
        console.log(array.toString());
    }



}