-1

I'm learning and i want put various values to be verified but don't work the script only verify the first value "123-21"

 function getInputValue(){
                
                var inputVal = document.getElementById("myInput").value;
                
                
                if (inputVal == "123-21"&&"154-2"&&"1235-1"){
                  document.getElementById("alertaValidacion").innerHTML = "El Producto que usted Compro es <strong>Original.</strong>";
                  document.getElementById("alertaValidacion").className = " alert alert-success";
                } 
                else {
                  document.getElementById("alertaValidacion").innerHTML = "El producto que adquirio <strong>No es Original. </strong> podria ser una falsificacion";
                  document.getElementById("alertaValidacion").className = " alert alert-danger";
                }
                
            }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<center>
        <input type="text" placeholder="Codigo Producto" id="myInput">
          <button class="btn btn-success" type="button" onclick="getInputValue();">Validar</button>
    </center>
        
        <div class="row">
        <div class="col-sm">
           
        </div>
        <div class="col-sm">
          <p  id="alertaValidacion">
            
            </p>
        <script>
           
        </script>
        </div>
        <div class="col-sm">
           
        </div>
        
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
      </div>
Deepak Rai
  • 2,103
  • 3
  • 19
  • 34
  • 1
    Does this answer your question? [Javascript: The prettiest way to compare one value against multiple values](https://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – CBroe Sep 10 '20 at 08:50
  • 1
    To understand why what you tried did not work, go read up on [Operator precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence). – CBroe Sep 10 '20 at 08:51

2 Answers2

1

Take a look at the snippet.


inputVal === "123-21" || inputVal === "154-2" || inputVal === "1235-1" checks if the inputVal-variable is equivalent to "123-21", "154-2" or "1235-1".

function getInputValue() {
  var inputVal = document.getElementById("myInput").value;
  if (inputVal === "123-21" || inputVal === "154-2" || inputVal === "1235-1") {
    document.getElementById("alertaValidacion").innerHTML = "El Producto que usted Compro es Original.";
    document.getElementById("alertaValidacion").className = "alert-success";
  } else {
    document.getElementById("alertaValidacion").innerHTML = "El producto que adquirio No es Original. podria ser una falsificacion";
    document.getElementById("alertaValidacion").className = "alert-danger";
  }
}

getInputValue();
.alert-success {
  color: green;
}

.alert-danger {
  color: red;
}
<input id="myInput" type="text" value="123-21" oninput="getInputValue()"/>
<p id="alertaValidacion"></p>
jns
  • 1,205
  • 1
  • 4
  • 17
0

The first if statement you have split up your statement like so

if (inputVal === "123-21" || inputVal === "154-2" || inputVal === "1235-1"){