0

I have this JS code, I need to compare the input value with the array, if the input value match with some value in the array then show the related message, but I can't get the array values and compare them with my input value.

var invalidkeyreservation = ['ABCDEF','GHIJK','LMNOP'];

    if ($("input.reservationkey").val() === invalidkeyreservation) {
        BootstrapDialog.show(return $content;}




    } else{
        window.location.href = "/branches/Cancelaciones/Seleccion.html";
    }
AlonsoCT
  • 177
  • 1
  • 4
  • 18
  • you're comparing the value with the array reference directly... have you tried looping through the elements in the array and checking them one by one? – Patrick Barr Jul 07 '17 at 18:23
  • What do you mean by *the array match with the input*? Do you want to check if the array contains the user's input? – PeterMader Jul 07 '17 at 18:25
  • 1
    ['ABCDEF','GHIJK','LMNOP'].indexOf($("input.reservationkey").val()) != -1 – gaetanoM Jul 07 '17 at 18:25
  • @PeterMader ahmm I need to check if the user's input value contains any of the arrays values. – AlonsoCT Jul 07 '17 at 18:27
  • Possible duplicate of [How do I check if an array includes an object in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – PeterMader Jul 07 '17 at 18:31

4 Answers4

1

This is what .indexOf() is for.

var invalidkeyreservation = ['ABCDEF','GHIJK','LMNOP'];

if (invalidkeyreservation.indexOf($("input.reservationkey").val()) > -1) {
  BootstrapDialog.show(return $content;}
} else{
  window.location.href = "/branches/Cancelaciones/Seleccion.html";
}
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
1

Maybe you want to use includes:

var invalidkeyreservation = ['ABCDEF','GHIJK','LMNOP'];

if (invalidkeyreservation.includes($("input.reservationkey").val())) {
    BootstrapDialog.show(return $content;}
} else{
    window.location.href = "/branches/Cancelaciones/Seleccion.html";
}

Obs: If you are targeting to old browsers, there is polyfill available, or just use indexOf, as shown in the other answer.

Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46
1

You should be able to see if one of the elements in the array includes any of the string value, like so:

ES6

const invalidkeyreservation = ['ABCDEF','GHIJK','LMNOP'];

if(invalidkeyreservation.some(key => key === $("input.reservationkey").val()) {
    BootstrapDialog.show(return $content);
} else{
    window.location.href = "/branches/Cancelaciones/Seleccion.html";
}

ES5

var invalidkeyreservation = ['ABCDEF','GHIJK','LMNOP'];

if(invalidkeyreservation.indexOf($("input.reservationkey").val()) > -1) {
    BootstrapDialog.show(return $content);
} else{
    window.location.href = "/branches/Cancelaciones/Seleccion.html";
}
Jason Bellomy
  • 442
  • 2
  • 7
0

try like

var toCheck = $("input.reservationkey").val().trim().toUpperCase();
if (invalidkeyreservation.includes(toCheck )) {
 //your code with the condition
}

Hope, it helps

Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32