Testing for user input does not match CSV value using the $.each method

Testing for user input does not match CSV value using the $.each method

Hi,

I am having difficulty finding a solution for this problem.

I am using AJAX to pull in data from a CSV file that checks user input from a text box against an ID column in the CSV file and outputs the row from the CSV file to screen if a match is found.

I am using the $.each method to check all rows in the CSV file and everything works as expected until I add some code within the $.each method to check when user input does not match a row in the CSV file. When this happens I want to out put an error message to the user. 

I am using a simple If else statement to check: if a match exists then output data in a table row or if no match exists then output an error message to the user but when I add the else statement for no match the code fails even when a correct id is entered.

Can you tell me from my code where I'm going wrong please? Thank you.

  1. // Search for STB Version ID match from userInput and display table row
  2.     var populateDataTable = function(dataOutput) {

  3.         if (userInput.val().length === 6 && userInput.val() !== 'Enter Version Number') {
  4.             var html = [];
  5.             $.each(dataOutput.results.rows, function(key, value) {

  6.                 var stbVersionId = $.trim(value['STB Version ID']),
  7.                     keySearch = $.trim(userInput.val());

  8.                 if (keySearch === stbVersionId) {
  9.                     console.log('Valid');
  10.                     var rowHTML = ['<tr class="row">'];
  11.                     rowHTML.push('<td>' + value['Model Number'] + '</td>');
  12.                     rowHTML.push('<td>' + value['Box type'] + '</td>');
  13.                     rowHTML.push('<td>' + value['Manufacturer'] + '</td>');
  14.                     rowHTML.push('<td>' + value['Name/Style'] + '</td>');
  15.                     rowHTML.push('<td>' + value['Operating System Version'] + '</td>');
  16.                     rowHTML.push('<td>' + value['Manufacturer'] + '</td>');
  17.                     rowHTML.push('</tr>');

  18.                     html.push(rowHTML.join(''));


  19.                     noData.hide();
  20.                     return false;

  21.                 } else if (keySearch !== stbVersionId) {
  22.                     console.log('Invalid');

  23.                     var userInputError = userInput.val().toUpperCase();

  24.                     noData.html(
  25.                         '<p>You Entered <span class="error">' + userInputError + '</span><br />' +
  26.                         'No match found - Please enter a valid STB Version ID</p>'
  27.                     );
  28.                     userInput
  29.                         .blur()
  30.                         .addClass('error')
  31.                         .val('Please enter a valid STD number');

  32.                     return false;
  33.                 }
  34.                 return;
  35.             });
  36.             $('#dataTable tbody').html(html.join(''));
  37.             userInput.blur();
  38.         }
  39.     };