JavaScript - Simple Leap Year Checker

In this tutorial we will create a Simple Leap Year Checker using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. This code will render application and display the form. 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>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.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 Leap Year Checker</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-3"></div>
  17.                 <div class="col-md-6">
  18.                         <div class="form-group">
  19.                                 <input type="number" id="year" class="form-control" placeholder="Enter a year here..."/>
  20.                         </div>
  21.                         <center><button class="btn btn-primary" onclick="yearCheck(this);" id="check">Check</button></center>
  22.                         <center><button class="btn btn-success" onclick="reset(this);" style="display:none;" id="reset">Reset</button></center>
  23.                         <br />
  24.                         <div id="result"></div>
  25.                 </div>
  26.         </div>
  27. <script src="js/script.js"></script>
  28. </body>
  29. </html>

Creating the Script

This code contains the script of the application. This code will check whether the year has a leap year. To do this just copy write the code as shown below inside the text editor and save it inside the js directory as script.js.
  1. function yearCheck(btn){
  2.         var year = document.getElementById('year').value;
  3.        
  4.         if(year == ""){
  5.                 document.getElementById('result').innerHTML = "<center><label class='text-danger'>Please enter a valid year</label></center>";
  6.         }else{
  7.        
  8.                 if(!(year % 4) && (year % 100) || !(year % 400)){
  9.                         document.getElementById("result").innerHTML = "<div class='alert alert-success' style='text-align:center;'>Year <label class='text-primary'>"+year+"</label> is a LEAP YEAR</div>";
  10.                 }else{
  11.                         document.getElementById("result").innerHTML = "<div class='alert alert-warning' style='text-align:center;'>Year <label class='text-danger'>"+year+"</label> is a NOT LEAP YEAR</div>";
  12.                 }
  13.                
  14.                 btn.style.display = 'none';
  15.                 document.getElementById('reset').style.display = '';
  16.                 document.getElementById('year').setAttribute('readonly', 'readonly');
  17.         }
  18. }
  19.  
  20. function reset(btn){
  21.         document.getElementById('year').value = "";
  22.         document.getElementById("result").innerHTML = "";
  23.        
  24.         btn.style.display = 'none';
  25.         document.getElementById('check').style.display = '';
  26.         document.getElementById('year').removeAttribute('readonly');
  27. }
There you have it we successfully created a Simple Leap Year Checker using JavaScript. I hope that this very 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