0

Okay, this may be hard to explain.
The Passwords don't work the usernames do.
I am reading from a text file.

"username,password" is the structure for the below is the text file

John,BOL12345
Mary2,BOL77777
Anna,BOL54321
test,BOL12345

The top 3 do not work alone i only need the top 3
but once i add the "test,BOL12345"
the password BOL12345 does work
but without the "test,BOL12345" The password "BOL12345" does not work or any of the other ones
I am doing this all in javascript below will be the code snippet.. please ask any questions as i do not understand why this happens.

The JavaScript Below
The "lines" = the text file above

    lines = x.responseText.split("\n");
    for (i=0; i < lines.length; i++)
    {
        test1 = lines[i].split(",")
        username.push(test1[0]);
        password.push(test1[1]);

    }
    var tempUsername = document.getElementById('username').value;
    var tempPassword = document.getElementById('password').value;
    var arraycontainsusername = (username.indexOf(tempUsername) > -1);
    var arraycontainspassword = (password.indexOf(tempPassword) > -1);
    alert(password);
    if (arraycontainsusername && arraycontainspassword) {
        window.location.href = "listing.htm";
    };
Vaughan D
  • 149
  • 1
  • 1
  • 15
  • some context would be nice... what are you trying to do? – Nicolas Straub Oct 17 '14 at 04:22
  • You should use indexOf if you have the same password for more than one user, but that still does not explain this. @NicolásStraubValdivieso what do you not understand? – mplungjan Oct 17 '14 at 04:24
  • Get the passwords of the top 3 to work without having "test,BOL12345" being in the text file. – Vaughan D Oct 17 '14 at 04:25
  • Can you console.log the arrays? Perhaps you are missing a \n – mplungjan Oct 17 '14 at 04:27
  • @mplungjan i only have more then one to prove that the password works..but none of the other ones work. only the test user...which is just a text – Vaughan D Oct 17 '14 at 04:28
  • i did alert on the "password" array and got "BOL12345 ,BOL77777 ,BOL54321 ,BOL12345 – Vaughan D Oct 17 '14 at 04:29
  • @mplungjan just trying to make sense of the purpose of these lines of code, where and how they are executed mainly (a lot of times this goes in an event that isn't properly bound, and you're comment makes me think the file might use \r\n instead of \n, or something like that). Mainly prodding like any engineer would do :) – Nicolas Straub Oct 17 '14 at 04:30
  • @VaughanD what about alerting when the array doesn't have the last line? – Nicolas Straub Oct 17 '14 at 04:31
  • @NicolásStraubValdivieso without the test line "BOL12345 ,BOL77777 ,BOL54321" A screencap of the alert http://puu.sh/cfqVb/5470c3655d.png – Vaughan D Oct 17 '14 at 04:32
  • can't reproduce the problem.. everything working fine here.. http://jsfiddle.net/mwh72dfp/ – Abdul Jabbar Oct 17 '14 at 04:39
  • @AbdulJabbar The problem i needed to split on \r as well as \n, but thank you for attempting to fix the problem. – Vaughan D Oct 17 '14 at 04:43

1 Answers1

1

Educated guess: your file is using \r\n. since you're splitting by \n the \r is left in and corrupts each string. try splitting by \r\n and see what happens. That would explain why adding the last line would work, since there's no newline at the end there won't be a trailing character to mess up the indexOf search.

different operating systems handle text files differently. Windows uses CRLF (Carriage Return Line Feed) to jump to the next line, while *NIX variants use LF. old MacOS versions use CR. Your code was assuming the file came from a *NIX environment, where LF (or \n) is the norm, when it came from a windows environment, where CRLF (or \r\n) is the norm (not accurate since you can make text files with LF in windows and with CRLF in *NIX, buy you get the picture).

To handle all cases correctly, I'd recommend normalizing the string before working on it:

x.responseText.replace(/\r\n|\r(?!\n)/g, '\n').split('\n');

that seemingly chinese string in the middle is actually a regular expression that matches either \r\n or \r (but only when \r isn't followed by \n). this way you can replace all your CRLFs and CRs to LF and handle text coming from any environment.

you can simplify that regex because of the order of the tokens, to /\r\n|\r/, but I'm leaving it in because it illustrates a neat concept (lookaheads - that bit (?!\n) says if and only if not immediately followed by a \n). With that said /\r\n|\r/ will perform better, especially when handling large files

Nicolas Straub
  • 3,381
  • 6
  • 21
  • 42
  • 1
    Thanks that seems to have worked. Do you think that you could explain what the \r is doing in a little more depth so i can understand easier for further use, please – Vaughan D Oct 17 '14 at 04:37