JavaScript - Simple Leap Year Checker
Submitted by razormist on Wednesday, January 23, 2019 - 13:21.
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...
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!!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6">
- <div class="form-group">
- <input type="number" id="year" class="form-control" placeholder="Enter a year here..."/>
- </div>
- <br />
- </div>
- </div>
- </body>
- </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.- function yearCheck(btn){
- var year = document.getElementById('year').value;
- if(year == ""){
- document.getElementById('result').innerHTML = "<center><label class='text-danger'>Please enter a valid year</label></center>";
- }else{
- if(!(year % 4) && (year % 100) || !(year % 400)){
- 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>";
- }else{
- 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>";
- }
- btn.style.display = 'none';
- document.getElementById('reset').style.display = '';
- document.getElementById('year').setAttribute('readonly', 'readonly');
- }
- }
- function reset(btn){
- document.getElementById('year').value = "";
- document.getElementById("result").innerHTML = "";
- btn.style.display = 'none';
- document.getElementById('check').style.display = '';
- document.getElementById('year').removeAttribute('readonly');
- }
Add new comment
- 62 views