How do I read a .txt file in javascript line by line?


Good, I have a text file with several possible passwords saved like this:



There are many more but it is to teach where the shots go. I do not know how to read it line by line and compare it with the password that the user is entering. The code I have is this (to read the file, taken from another Stack Overflow thread):

$(document).ready(function() {
  $("#pass").on("change", function() {
    var path = "../DATUAK/toppasswords.txt";

<script src=""></script>
<div id='page-wrap'>
  <span><a href='../HTML5/layout.html'>Atzera</a></span> <br><br>

  <h2>Quiz: crazy questions</h2>

    <form id="galderenF" name="galderenF" method="post" action="signUp.php" onsubmit="return balidatu()">

      <label for="posta"><strong>Posta (*):</strong></label>
      <input type="email" name="posta" id="posta" placeholder="Zure posta" pattern="[a-zA-Z]{2,}[0-9]{3}@ikasle\.ehu\.(eus|es)" required/><br>

      <label for="deitura"><strong>Deitura (*):</strong></label>
      <input type="text" name="deitura" minlength="10" id="galdera" placeholder="Zure deitura" required/><br>

      <label for="nick"><strong>Nick (*):</strong></label>
      <input type="text" name="nick" id="erantzunZuzena" placeholder="Zure nick-a" required/><br>

      <label for="pasahitza"><strong>Pasahitza (*):</strong></label>
      <input type="password" name="pasahitza" minlength="6" id="pass" placeholder="Pasahitza" required/><br>

      <label for="pasEr"><strong>Pasahitza Errepikatu (*):</strong></label>
      <input type="password" name="pasahitzaRep" minlength="6" id="passC" placeholder="Pasahitza errepikatu" required/><br>

      <input class="botoia" type="submit" id="botoia1" value="Erregistratu" />
      <input class="botoia" type="reset" id="botoia2" value="Reset" />
      <div id="aster"><strong> * | Nahitaezko hutsuneak </strong></div><br><br>

    <div id="ezkutatu1" style='display:block'>Posta egiaztatzen... </div>
    <div id="erakutsiEgiaBada1" style='display:none'> Posta onargarria da </div>
    <div id="erakutsiGezurraBada2" style='display:none'> Posta existitzen da WSan </div>

    <div id="ezkutatu2" style='display:block'> Pasahitza egiaztatzen... </div>
    <div id="erakutsiEgiaBada2" style='display:none'> Pasahitza onargarria da </div>
    <div id="erakutsiGezurraBada2" style='display:none'> Pasahitza ez da onargarria da </div>

The problem is that I can not read it line by line, which is what I want to compare it with the password line by line.
I need it in javascript or in jQuery, but in PHP it would be like this:

    $pasahitzak = fopen('../DATUAK/toppasswords.txt','r');
    while($linea = fgets($pasahitzak) && $ondo==TRUE){
        echo $linea.'<br/>';


asked by Mikel Molinuevo 21.11.2017 в 15:48

1 answer


Simply, what you could do is separate the string by line break ( \n ) and go through the resulting list:

var input = myForm.myInput;
var reader = new FileReader;

input.addEventListener('change', onChange);

function onChange(event) {
  var file =[0];
  reader.onload = onLoad;

function onLoad() {
  var result = reader.result; // Obtienes el texto
  // En tu ejemplo lo obtienes de una petición HTTP
  var lineas = result.split('\n');
  // o lineas.forEach(function(linea){ ... })
  // o lineas.find(function(linea){ return linea == contraseña })
  for(var linea of lineas) {
    console.log('[linea]', linea)
    //if(linea === passwordBuscar) {
      // Encontraste contraseña
<form name="myForm">
  <input name="myInput" type="file">


I do not see in your code where you get the text of the HTTP request, you can do it using the callback success or as returns a Promise, you can use the then method:

  url: '',
}).then(function(data) {
  console.log(data); // Obtienes el texto
<script src=""></script>
answered by 21.11.2017 / 17:42