How can I put an array in a JavaScript variable and compare it with the value in the text field?

1

I have the code below and I need to get several different values at once. One way I thought it was in array else does not work. After Ajax it brings me the results in id zpl , if the result comes empty it returns the warning that does not have the value in the bank. So that's fine, but I'd also like that if the result is SD10 and SD11 and so on, the same message of the empty condition also appears, but I wish I could not put several ous || in the condition and put the values inside a variable in array and make the comparison in

var variavel = new array('valor1','valor2');
document.getElementById('zpl').value == variavel;

Can anyone help me?

I would like to take several values and put inside a variable and at the time of doing the validation with the command document.getElementById('valor do campo').value == the variable that has these values. I've tried:

var arr = new array("s10","s11","s12","s13");

document.getElementById('valor do campo').value = arr

That is, it will compare the value of the text field of the page with the values of the variables stored in the variable arr .

Does anyone know how to do this in JavaScript?

$.ajax({
    type: "POST",
    url: "label_query.php",
    data: {
        serial: $('#serial').val(),
        combox: $("#combox").val()
    },
    success: function(data) {
        $('#zpl').html(data);

        var arr = new array('SD10', 'SD11', 'SD12');

        if (document.getElementById('zpl').value == '' || document.getElementById('zpl').value == arr) {

            alert('NÂO HÁ REGISTRO DESSE SERIAL NO BANCO DE DADOS!');
            document.getElementById('serial').value = '';
            document.getElementById("bSubmit").disabled = false;
            document.getElementById("bSubmit").value = "Enviar";
            document.getElementById("serial").focus();
            return false;
        } else {
            criarArquivo();
            document.getElementById("bSubmit").disabled = false;
            document.getElementById("bSubmit").value = "Enviar";
            document.getElementById("serial").focus();
        }
    
asked by anonymous 05.05.2016 / 13:25

3 answers

1

You can use indexOf as follows:

function contem() {
  var valor = document.getElementById("valor").value;
  var arr = ['ASD', 'DEF', 'GHI'];
  document.getElementById('result').innerHTML = (arr.indexOf(valor) > -1 ? 'Contem o valor digitado' : 'Nao Contem');
}
<input type="text" id="valor">
<button onclick="contem()">Testar</button>
<div id="result"></div>

A very important detail to mention is that this architecture could be better planned, I explain:
When the backend does not find a record, it ALWAYS must return an error and never a success. Home Then you would not have to deal with different solutions in the same way. It's just an improvement tip.

    
05.05.2016 / 13:38
1

I would only use this way:

['s10','s11','s12','s13'].indexOf(document.getElementById('zpl').value) !== -1;

The result of this line would be true if the value of the field is any of these in the array, otherwise, it would be false

    
07.12.2017 / 17:29
0

You must transform the array into a string and then compare it to the value

Replace:

var arr = new array("s10","s11","s12","s13");

By

var arr = ['s10', 's11', 's12', 's13'].join("");

Then

document.getElementById('zpl').value == arr;
    
13.01.2017 / 12:39