473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Aug 28 '07 #1
9 7284
phvfl
173 Expert 100+
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.
Aug 28 '07 #2
I saw on some website there was an example of adding a text file into an array.

Ex:
Expand|Select|Wrap|Line Numbers
  1. var items = new Array();
  2.       var itemLabels = new Array();
  3.       items[0] = ['file1.txt','file2.txt'];      // List of files in group 1
  4.       itemLabels[0] = ['File 1','File 2'];      // Description of files in group 2
  5.  
Now I am not sure what this actually would do, but does the line
items[0] = ['file1.txt','file2.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?
Aug 28 '07 #3
phvfl
173 Expert 100+
Expand|Select|Wrap|Line Numbers
  1.       var items = new Array();
  2.       var itemLabels = new Array();
  3.       items[0] = ['file1.txt','file2.txt'];      // List of files in group 1
  4.       itemLabels[0] = ['File 1','File 2'];      // Description of files in group 2
  5.  
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.
Aug 28 '07 #4
pbmods
5,821 Expert 4TB
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.
Aug 28 '07 #5
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?
Aug 28 '07 #6
pbmods
5,821 Expert 4TB
Heya, Darth.

Have a look at this article.
Aug 28 '07 #7
phvfl
173 Expert 100+
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.
Aug 28 '07 #8
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...knowing 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.
Aug 28 '07 #9
phvfl
173 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. var req;
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. function makeRequest(password){
  2.     req=null;
  3.  
  4.     if (window.XMLHttpRequest){
  5. //Everything but IE
  6.         req = new XMLHttpRequest();
  7.     }
  8.     else if(window.ActiveXObject){
  9. //IE
  10.         req = new ActiveXObject("Microsoft.XMLHTTP");
  11.     }
  12.  
If req is not null then it has been set correctly so process the request.

Expand|Select|Wrap|Line Numbers
  1.     if (req!=null){
  2.         var url;
  3.         url="/url/for/passwordtest.aspx?pw="+password;
  4.         req.onreadystatechange=state_Change;
  5.         req.open("GET",url,true);
  6.         req.send(null);
  7.     }
  8. }
  9.  
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 onreadystatechange of the req object changes a function called state_Change will be called:

Expand|Select|Wrap|Line Numbers
  1. function state_Change(){
  2.     // if xmlhttp shows "loaded"
  3.     if (req.readyState==4){
  4.     // if "OK"
  5.         if (req.status==200){
  6.             var response;
  7.             response=req.responseText;
  8.             //handle response.
  9.         }
  10.     }
  11. }
  12.  
The complete javascript in one block is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. var req;
  3.  
  4. function makeRequest(password){
  5.  
  6.     req=null;
  7.  
  8.     if (window.XMLHttpRequest){
  9. //Everything but IE
  10.         req = new XMLHttpRequest();
  11.     }
  12.     else if(window.ActiveXObject){
  13. //IE
  14.         req = new ActiveXObject("Microsoft.XMLHTTP");
  15.     }
  16.  
  17.     if (req!=null){
  18.         var url;
  19.         url="/url/for/passwordtest.aspx?pw="+password;
  20.         req.onreadystatechange=state_Change;
  21.         req.open("GET",url,true);
  22.         req.send(null);
  23.     }
  24. }
  25.  
  26. function state_Change(){
  27.     // if xmlhttp shows "loaded"
  28.     if (req.readyState==4){
  29.     // if "OK"
  30.         if (req.status==200){
  31.             var response;
  32.             response=req.responseText;
  33.             //handle response.
  34.         }
  35.     }
  36. }
  37.  
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:

Expand|Select|Wrap|Line Numbers
  1. 'set a variable for the password value
  2. Dim pw = request.QueryString("pw")
  3.  
  4. 'test against the file
  5. if matched then
  6. response.write("true")
  7. else
  8. response.write("false")
  9. end if
  10.  
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
Aug 29 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Reply Via Newsgroup | last post by:
Folks, I'm pretty sure it can be done, and I'm doing pretty good with my javascript so I'm pretty sure I just need a quick point in the right direction... If my web page is referenced in the...
4
by: Mike | last post by:
I need to read a web a page and do a search on the page and gather information and put it into a text file. the web page is setup into a table, and displays information on files stuck in a queue....
4
by: eBob.com | last post by:
I want to read a web page. That is, I have a URL and I want to read the HTML and parse it and put the info into a data base. I've never done any TCP/IP programming. Thanks to a recent post...
4
by: Paul | last post by:
Hi everyone, I have created my first real life application, that runs in the system tray and monitors changes to the clipboard, then depending on the content it may search our stock file and...
3
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library...
6
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is...
60
by: harshal | last post by:
Hi all, Can we read the stack frame's of the current process. as we know that whenever a function call is made in c new functions stack frame is created and pushed on to the stack. and when the...
0
by: alivip | last post by:
Is python provide search in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name camry and file name corola,...
11
by: alivip | last post by:
how to ingrate my code to read text in in parent folder contain sub folders and files for example folder name is cars and sub file is Toyota,Honda and BMW and Toyota contain file name Camry and...
3
by: =?Utf-8?B?UGVycmlud29sZg==?= | last post by:
Not sure where to post this... Found some interesting behavior in Windows Search (Start =Search =All files and folders =search for "A word or phrase in the file:"). This applies to XP and maybe...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.