JavaScript - Simple Generate Password For User

Submitted by: 
Visitors have accessed this post 257 times.

In this tutorial we will create a Simple Generate Password For User using JavaScript. This code will create a random password for user each time the button is click. This code use onclick function to initiate a javascript built-in Math function to randomly mix the characters in order to generate a newly string. This program is a user-friendly feel free to use it in your system.

We will use JavaScript to allow you to implement complex things on web pages. It enables you to create dynamically updating content and add some interactive visual for the user transactions.

Getting Started:

This is the link for the bootstrap that i used for the layout design

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar navbar-brand" href="<a href=""></a>
  12. " rel="nofollow">"></a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">Javascript - Simple Generate Password For User</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-2"></div>
  20. <div class="col-md-8">
  21. <form class="form-group">
  22. <label>Password Length:</label>
  23. <center><input type="text" id="plength" class="form-control" style="width:200px;"/></center>
  24. <br />
  25. <center><button type="button" class="btn btn-sm btn-success" onClick="getPassword(this)"/><span class="glyphicon glyphicon-random"></span> Create Password</button> <a href="" class="btn btn-sm btn-danger"/><span class="glyphicon glyphicon-reset"></span> Get Password Again</a></center>
  26. <br />
  27. <input type="text" id="password" placeholder="My Password" class="form-control" readonly = "readonly"/>
  28. </form>
  29. </div>
  30. </div>
  31. </body>
  32. </body>
  33. <script src="js/script.js"></script>
  34. </html>

Creating the Script

This code contains the script of the application. This code will create a password when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  1. var chars = "abcdefghijklmnopqrstubwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  2. var password = '';
  5. function getPassword(btn){
  6. var plength = document.getElementById('plength').value;
  7. if(plength == 0){
  8. alert("Please enter something first");
  9. }else{
  10. document.getElementById('password').value = createPassword(plength);
  12. btn.setAttribute('disabled', 'disabled');
  13. }
  14. }
  16. function createPassword(plength){
  17. password='';
  18. for(i=0; i<plength; i++){
  19. password+=chars.charAt(Math.floor(Math.random()*chars.length));
  20. }
  22. return password;
  23. }

There you have it we successfully created a Simple Generate Password For User using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.