2

I've uploaded a CSV-file to an HTML page via javascript. The CSV rows are: name and email-address, e.g. rambo,[email protected].

How to SEARCH the 'name' from these loaded CSV-file?

Also, one of the data is an email-address and I want to send a mail to that email-address. Is that value retrieved to a variable?

My code to search each elements:

function Search() {
 var fileUpload = document.getElementById("fileUpload");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;
if (regex.test(fileUpload.value.toLowerCase())) {
    if (typeof (FileReader) != "undefined") {
        var reader = new FileReader();
        reader.onload = function (e) {
          var table = document.createElement("table");
var rows = e.target.result.split("\n");
for(var i = 0; i < rows.length; i++)
{   
    var row = table.insertRow(-1); 
    var cells = rows[i].split(",");
    for(var j = 0; j < cells.length; j++)
    {
        var cell = row.insertCell(-1); 
      //  cell.innerHTML = cells[j];


    // Here repeated checkboxes:
    var radio = document.createElement('input');
    radio.type = 'checkbox';
    radio.name = 'check';

} 

var ser=document.getElementById("texts");
    if(cells[i].indexOf(ser))
    {
     alert("matches");
       cell.innerHTML = cells[i];
    }
    else
    {
     alert("unmatches");
    }

    var cell = row.insertCell(-1);
    cell.appendChild(radio);
    //cell.appendChild(button);
}
 var button = document.createElement('button');
    button.textContent = 'Send';
cell.appendChild(button);
button.onclick = function(){ alert();};

var dvCSV = document.getElementById("dvCSV");
dvCSV.innerHTML = "";
dvCSV.appendChild(table);
        }

        reader.readAsText(fileUpload.files[0]);

    } 
   }
}
1
  • the user inputs a text, it must checked with each CSV data(here it is based on 'name'). If matches it should display it. Commented May 18, 2015 at 5:24

1 Answer 1

1

Ad search: indexOf() is your friend here. This should give you a figure:

var table = $('#your-table'),
    searchstring = 'your-searchstring';
searchstring.toLowerCase();
for (var i = 0, cell; cell = table.cells[i]; i++) {
    if (cell.indexOf(searchstring)) {
        // I don't know what you want to do with the search-results...
        // ..but you can do it here.
    }
}

Ad email-address: you can add the address to a variable in your CSV-import:

var cells = rows[i].split(","),
    address = cells[1];

I'd suggest making an array addresses and fill it each row.

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

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.