Match string case sensitive value with array

Here is my code!!!
I have one input field And
One of the array
I’m matching Input field entered value with arrray’s list.
Prob : Unable to match capital value with array’s list.
e.g if user enter one10 value so this one should be match or if user enter ONE10 in capital letter then this value should be match too.

function myFunctiontwo(){
  var good = [
      "one10",
      "two10",
      "three10"
  ];
  var a = document.getElementById("code").value.split(' ');


  var foundPresent = a.some(elem => good.indexOf(elem) > -1);
  if(foundPresent === true){
    alert("correct");
      
  }else {
    alert("wrong");
      
  }
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    

    <input name="code" id="code" placeholder="code" required>
    <button id="submit" id="sumbit" onclick="myFunctiontwo()">GO</button>
</body>
</html>

36 thoughts on “Match string case sensitive value with array”

  1. You can use the toLowerCase() method here. Assuming all your elements in good are lowercase you can just do elem.toLowerCase()

    var foundPresent = a.some(elem => good.indexOf(elem.toLowerCase()) > -1);
    

    When not you also should convert the array elements toLowerCase()

    good = good.map(x => x.toLowerCase());
    

    In the snippet below I added the element "FOur20" to the array.

    As you can see four20 will give you the message correct

    function myFunctiontwo(){
      var good = [
          "one10",
          "two10",
          "three10",
          "FOur20"
      ];
    
      good = good.map(x => x.toLowerCase());
      console.log(good);
      
      var a = document.getElementById("code").value.split(' ');
    
    
      var foundPresent = a.some(elem => good.indexOf(elem.toLowerCase()) > -1);
      if(foundPresent === true){
        alert("correct");
          
      }else {
        alert("wrong");
          
      }
    }
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    </head>
    <body>
        
    
        <input name="code" id="code" placeholder="code" required>
        <button id="submit" id="sumbit" onclick="myFunctiontwo()">GO</button>
    </body>
    </html>
    Reply
  2. you can always convert to lowercase and check if it exists in the good array.
    
        var foundPresent = a.some(elem => good.indexOf(elem.toLowerCase()));
        if(foundPresent === true){
            alert("correct");    
        }
        else {
           alert("wrong");
        }
       
    
    Reply

Leave a Comment