-1

I have a list that I want to compare to a input value and when the input value doesn't match anything in my list I want to make an alert(Album don't exist).

Here my search bar:-

<input id="search" type="text" name="text" placeholder="Album name">
     <button onclick="sendToPage()">Search</button>`

And here my list:-

var validSearch = ["The Stage", "Hail to the King", "Nightmare", "Avenged Sevenfold", "City of Evil", "Waking the Fallen", "Sounding the Seventh Trumpet"];

I have read a suggestion but can´t get my head around it.. :)

4
  • 1
    Can you share your current code? Commented Nov 23, 2016 at 14:34
  • 1
    Possible duplicate of Javascript/jQuery compare input value to array Commented Nov 23, 2016 at 14:34
  • validSearch.includes( input.value ) Commented Nov 23, 2016 at 14:34
  • validSearch.indexOf(document.getElementById("search")) > -1 Commented Nov 23, 2016 at 14:38

3 Answers 3

1

You can use indexOf on arrays. It returns the index of the element matched. Only thing I'd suggest is you turn the search array lower or upper case first, so matching doesn't rely on the person typing it EXACTLY as you have defined it with every capital letter exactly as you wrote it.

function match(elem) {
    const validSearch = [
                    "The Stage", 
                    "Hail to the King", 
                    "Nightmare", 
                    "Avenged Sevenfold", 
                    "City of Evil", 
                    "Waking the Fallen", 
                    "Sounding the Seventh Trumpet"];
      
    /** make search case insentive **/
    let searchKeys = validSearch.map( e => e.toLowerCase() );
    /** search the input **/
    let index = searchKeys.indexOf(elem.value.toLowerCase());
        
    /** if not matched **/
    if(index == -1) {
       window.alert("Album does not exist. Please try again");
    }
    else {
        let album = validSearch[index];
        window.alert("FOUND IT! " + album);
    }
}
+function() {
    let search = document.getElementById('search');
    document.getElementById('searchbutton').addEventListener('click', (e) => {match(search)});
}();
<input id="search" type="text" name="text" placeholder="Album name">
<button id="searchbutton">Search</button>

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for your help, I really appreciate it!
Can you explain these code blocks a little more? Do not understand why you created an empty list and then was the letter c comes from. And why you have a parameter named elem? /** make search case insentive / var searchKeys = []; for(c=0;c<validSearch.length;c++) { searchKeys[c] = validSearch[c].toLowerCase(); } / search the input **/ var index = searchKeys.indexOf(elem.value.toLowerCase());
For the loop: w3schools.com/js/js_loop_for.asp Also, the empty list is so it can store the "case insensitive variant" of the entries. We need it initialise it first before we can fill it. Otherwise javascript doesn't know it's an array.
Just a warn: Array.prototype.indexOf is not a part of legacy IE js.
@KrzysztofDelestowicz add an id to the button, the document.getElementById('your_button_id_here').on('click', (e) => { match(document.getElementById('search'));}); and I would change the for loop into a map.var searchKeys = validSearch.map( e => e.toLowerCase() );
|
1

//Your array of valid searches to compare against
var validSearch = ["The Stage", "Hail to the King", "Nightmare", "Avenged Sevenfold", "City of Evil", "Waking the Fallen", "Sounding the Seventh Trumpet"];

//The click handler function
var sendToPage = function(e){
  //Get the input value by finding the element by its ID
  var album = document.getElementById('search').value;
  
  //Check if the value is in the array
  if(validSearch.indexOf(album) < 0){
    alert("Album don't exist");
  }else{
    alert("Album exists");
  }
}
<input id="search" type="text" name="text" placeholder="Album name">
<button onclick="sendToPage()">Search</button>`

Comments

1

You can try using regex instead: can search for case insensitive keywords and can find the closest item with limited keywords (in case user dont know how to spell correctly );

var validSearch = ["The Stage", "Hail to the King", "Nightmare", "Avenged Sevenfold", "City of Evil", "Waking the Fallen", "Sounding the Seventh Trumpet"];

function sendToPage() {
  //get value and trim for unnecesary spaces, and set variable
  var q = document.getElementById("search").value.trim(),
    re, result;

  //return if textbox is empty
  if (!q.length > 0) {
    return;
  }

  //set RegExp (indexOf is faster but Case Sensitive)
  re = new RegExp(".*" + q.replace(/\s/g, "\\s") + ".*", "ig");

  //start searching
  validSearch.some(function(v) {
    result = re.exec(v);
    if (result) {
      return true;
    }
  })

  //if match 
  if (result !== null) {
    alert("Found it!! - " + result[0]);
  } else {
    alert("Book not Found!! - " + q);
  }

  //refresh input box
  document.getElementById("search").value = "";

}
<input id="search" type="text" name="text" placeholder="Album name">
<button onclick="sendToPage()">Search</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.