Read / Search Text File in JS

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DarthPeePee
    New Member
    • Aug 2007
    • 6

    Read / Search Text File in JS

    Hello Everyone,

    I posted yesterday about a password strength meter I am working on. Currently I have a function that checks if common passwords are used in the password itself (eg: qwerty, welcome, password1, etc...).

    On the web, I found a large .txt file that contains over hundreds of commonly used passwords that I would like to implement into my PW checker (but would take up too much space by adding hundred of If-Else statements.

    My question is...

    Is there a way in JS to read the .txt file, search through the file, and check to see if any of the common PW's in the list match the current PW?

    or

    What would be an easy way to do so?

    Any help would be greatly appreciated.
  • phvfl
    Recognized Expert New Member
    • Aug 2007
    • 173

    #2
    Hi,

    The contents if the file would need to be put into a variable as you would not be able to load the file from the client system (without AJAX). If you are going to use AJAX then it may be better to submit the password to a page using AJAX and then checking that against the file on the server and returning whether a match was found or not.

    If you are not familiar with AJAX a good introduction is at http://www.w3schools.com/ajax/default.asp. If you need any more help let me know.

    Comment

    • DarthPeePee
      New Member
      • Aug 2007
      • 6

      #3
      I saw on some website there was an example of adding a text file into an array.

      Ex:
      [CODE=javascript]var items = new Array();
      var itemLabels = new Array();
      items[0] = ['file1.txt','fi le2.txt']; // List of files in group 1
      itemLabels[0] = ['File 1','File 2']; // Description of files in group 2
      [/CODE]

      Now I am not sure what this actually would do, but does the line
      items[0] = ['file1.txt','fi le2.txt']
      allow data from a text file to be stored into an array?

      If so, is there a way to search through an array and match it to a password variable?
      Last edited by pbmods; Aug 28 '07, 06:06 PM. Reason: Changed [CODE] to [CODE=javascript].

      Comment

      • phvfl
        Recognized Expert New Member
        • Aug 2007
        • 173

        #4
        Originally posted by DarthPeePee
        [CODE=javascript]
        var items = new Array();
        var itemLabels = new Array();
        items[0] = ['file1.txt','fi le2.txt']; // List of files in group 1
        itemLabels[0] = ['File 1','File 2']; // Description of files in group 2
        [/CODE]
        The code above just stores the names of the files into an array. It is not reading from the files. For security reasons javascript does not have any native file handling capabilities.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Darth.

          As long as the text files are stored on the server, you can use Ajax. Otherwise, you'll need to use a file upload script, which means you can probably do most of the hard work server-side instead.

          Comment

          • DarthPeePee
            New Member
            • Aug 2007
            • 6

            #6
            Thanks guys. I'm not too familiar with AJAX (...at all actually). Is this a simple process to do or is it really involved?

            One thing I would of liked is to not submit the page. Right now I have everything running directly on the page... The user types in a password, and the meter changes. I have around 20 if-else statements to check if the password is contained...

            If I am using AJAX, is there a way to have the code point to the server and check the file without having to submit anything?

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Darth.

              Have a look at this article.

              Comment

              • phvfl
                Recognized Expert New Member
                • Aug 2007
                • 173

                #8
                Darth,

                AJAX would not refresh the page and the page remains fully responsive.

                If the example provided is confusing or you still have questions let us know.

                Comment

                • DarthPeePee
                  New Member
                  • Aug 2007
                  • 6

                  #9
                  phvfl & pbmods,

                  Thanks for the links. Like I said, I am not familiar with AJAX at all, so I will definitely need to break this down and figure it out and see how to implement it with the javascript / HTML that I am currently using.

                  I partially understand the flow of the code, but syntax is a different issue. That is where I will run into problems...know ing what to use and when (and how).

                  This was just a general idea that I thought could be of use, but I think I may be a little over my head.

                  Comment

                  • phvfl
                    Recognized Expert New Member
                    • Aug 2007
                    • 173

                    #10
                    Darth,

                    I'll break down the code and hopefully you should be able to follow the flow a bit better. It can seem quite daunting at first.

                    Firstly to explain what AJAX does. Simply put AJAX uses javascript to send a request to a page and return the result. The return from this would be whatever is on the page. While all this happens the user can still use the page and is not aware of what is happening in the background. An example may help clarify this.

                    First you need to create a request variable:

                    [CODE=javascript]
                    var req;
                    [/CODE]

                    As ever IE and everything else use different items to implement the request so a quick if loop to determine the type of browser and then set req to the correct type:

                    [CODE=javascript]
                    function makeRequest(pas sword){
                    req=null;

                    if (window.XMLHttp Request){
                    //Everything but IE
                    req = new XMLHttpRequest( );
                    }
                    else if(window.Activ eXObject){
                    //IE
                    req = new ActiveXObject(" Microsoft.XMLHT TP");
                    }
                    [/CODE]

                    If req is not null then it has been set correctly so process the request.

                    [CODE=javascript]
                    if (req!=null){
                    var url;
                    url="/url/for/passwordtest.as px?pw="+passwor d;
                    req.onreadystat echange=state_C hange;
                    req.open("GET", url,true);
                    req.send(null);
                    }
                    }
                    [/CODE]

                    The url variable is the address for the page that will read from the text file and look for the password submitted in the pw parameter of the querystring. Whenever the onreadystatecha nge of the req object changes a function called state_Change will be called:

                    [CODE=javascript]
                    function state_Change(){
                    // if xmlhttp shows "loaded"
                    if (req.readyState ==4){
                    // if "OK"
                    if (req.status==20 0){
                    var response;
                    response=req.re sponseText;
                    //handle response.
                    }
                    }
                    }
                    [/CODE]

                    The complete javascript in one block is:

                    [CODE=javascript]

                    var req;

                    function makeRequest(pas sword){

                    req=null;

                    if (window.XMLHttp Request){
                    //Everything but IE
                    req = new XMLHttpRequest( );
                    }
                    else if(window.Activ eXObject){
                    //IE
                    req = new ActiveXObject(" Microsoft.XMLHT TP");
                    }

                    if (req!=null){
                    var url;
                    url="/url/for/passwordtest.as px?pw="+passwor d;
                    req.onreadystat echange=state_C hange;
                    req.open("GET", url,true);
                    req.send(null);
                    }
                    }

                    function state_Change(){
                    // if xmlhttp shows "loaded"
                    if (req.readyState ==4){
                    // if "OK"
                    if (req.status==20 0){
                    var response;
                    response=req.re sponseText;
                    //handle response.
                    }
                    }
                    }
                    [/CODE]

                    The variable response is used so that the response is in a variable. It is populated from the responseText property of the request. Code would need to be added to handle the response.

                    The only other thing required would be to write the aspx page that the request is being sent to. I won't go through all the code but it should flow something like:

                    Code:
                    'set a variable for the password value
                    Dim pw = request.QueryString("pw")
                    
                    'test against the file
                    if matched then
                    response.write("true")
                    else
                    response.write("false")
                    end if
                    The response value in the javascript would then be true or false depending on the result. Only the one word would be required on the aspx page (no HTML needed). Hope this helps

                    Comment

                    Working...