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.
- // Search for STB Version ID match from userInput and display table row
- var populateDataTable = function(dataOutput) {
- if (userInput.val().length === 6 && userInput.val() !== 'Enter Version Number') {
- var html = [];
- $.each(dataOutput.results.rows, function(key, value) {
- var stbVersionId = $.trim(value['STB Version ID']),
- keySearch = $.trim(userInput.val());
- if (keySearch === stbVersionId) {
- console.log('Valid');
- var rowHTML = ['<tr class="row">'];
- rowHTML.push('<td>' + value['Model Number'] + '</td>');
- rowHTML.push('<td>' + value['Box type'] + '</td>');
- rowHTML.push('<td>' + value['Manufacturer'] + '</td>');
- rowHTML.push('<td>' + value['Name/Style'] + '</td>');
- rowHTML.push('<td>' + value['Operating System Version'] + '</td>');
- rowHTML.push('<td>' + value['Manufacturer'] + '</td>');
- rowHTML.push('</tr>');
- html.push(rowHTML.join(''));
- noData.hide();
- return false;
- } else if (keySearch !== stbVersionId) {
- console.log('Invalid');
- var userInputError = userInput.val().toUpperCase();
- noData.html(
- '<p>You Entered <span class="error">' + userInputError + '</span><br />' +
- 'No match found - Please enter a valid STB Version ID</p>'
- );
- userInput
- .blur()
- .addClass('error')
- .val('Please enter a valid STD number');
- return false;
- }
- return;
- });
- $('#dataTable tbody').html(html.join(''));
- userInput.blur();
- }
- };