JavaScript - Simple Login App

In this tutorial we will create a Simple Login App using Javascript. This code can be used in validating some forms to prevent errors. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that has been used for the layout https://getbootstrap.com/ And this is the link for the jQuery https://jquery.com/

The Main Interface

This code contains the interface of the application. This code will render application and display the form that will be use in vakidation. 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.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <meta charset="UTF-8"  name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodster.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">Javascript - Simple Login App</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-3"></div>
  17.                 <div class="col-md-6">
  18.                         <form method="POST">   
  19.                                 <div class="form-group">
  20.                                         <label>Username:</label>
  21.                                         <input type="text" id="username" class="form-control"/>
  22.                                 </div>
  23.                                 <div class="form-group">
  24.                                         <label>Password:</label>
  25.                                         <input type="password" id="password" class="form-control"/>
  26.                                 </div>
  27.                                 <div id="error"></div>
  28.                                 <br />
  29.                                 <div class="form-group">
  30.                                         <button type="button" class="btn btn-primary form-control" onClick="Login()">Login</button>
  31.                                 </div>
  32.                         </form>
  33.                 </div>
  34.         </div>
  35. </body>
  36. <script src="js/script.js"></script>
  37. </html>
Next, we will create the home page after the user successfully login. Create another text file then write these block of codes inside the newly created text file then called it home.html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <meta charset="UTF-8"  name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodster.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">Javascript - Simple Login App</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-3"></div>
  17.                 <div class="col-md-6">
  18.                         <h2>Welcome! User</h2>
  19.                         <a href="index.html">Logout</a>
  20.                 </div>
  21.         </div>
  22. </body>
  23. <script src="js/script.js"></script>
  24. </html>

Creating the Script

This code contains the script of the application. This script will process the login information then check if the information is valid then will be directed to the home page. To do this just create a new directory called js then copy and write these block of codes as shown below inside the text editor and save it as script.js in the newly created directory.
  1. function Login(){
  2.         var username = document.getElementById('username');
  3.         var password = document.getElementById('password');
  4.        
  5.        
  6.         if(username.value == "" || password.value == ""){
  7.                 document.getElementById('error').innerHTML = "<center class='text-warning'>Please Complete The Required Field!</center>";
  8.         }else{
  9.                 if(username.value == "admin" && password.value == "admin"){
  10.                         document.getElementById('error').innerHTML = "";
  11.                         username.value = "";
  12.                         password.value = "";
  13.                         window.location = "home.html";
  14.                 }else{
  15.                         document.getElementById('error').innerHTML = "<center class='text-danger'>Invalid Username or Password</center>";
  16.                 }
  17.         }
  18. }
There you have it we successfully created a Simple Login App 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!!

Comments

really really bad example. Against all kinds of security.

Add new comment