All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Date Validation In JavaScript

Last Updated : Mar 11, 2024

Date Validation In JavaScript

In this tutorial we will show you the solution of date validation in JavaScript, this statement states that we have to check the given date is valid or not inside JavaScript.

Here, below we are going to show you an example with help of which we are able to easily understand that the given date is valid or not. Let’s see below.

Step By Step Guide On Date Validation In JavaScript :-

Here, we are going to show you how to check validation on date in JavaScript.

Let us see the example below for better understanding.

<!DOCTYPE HTML>
<html>
<head>
 <title>
  JavaScript Validation
 </title>
</head>
<body>
 <h1>
  Talkers Code - - JavaScript Validation
 </h1>
 <h2>
  Date validation in JavaScript
 </h2>
 <button onclick = "DateValidation()">
  Click Here to check date
 </button>
 <p id = "result">
 </p>
// JavaScript Code here
 <script>
  var result = document.getElementById('result');
  var date = new Date(" provide date here ");
  function DateValidation() {
   if (Object.prototype.toString.call(date)
         === "[object Date]")
   {
    if (isNaN(date.getTime())) {
     result.innerHTML = "Given date is invalid date .";
    }
    else {
     result.innerHTML = " Given date is a valid date.";
    }
   }
  }
 </script>
</body>
</html>
  1. Here, above we show you an example in JavaScript.
  2. For this first of all we here create a basic structure of html. For this we use html, head, title and body with script tag. We hope you know about the basic structure of html and which html tag is used for which concept.
  3. Now, when we look inside body tag. Here, we see some headings tag and a script tag. Heading tag again is a concept of html, whereas script relates to JavaScript.
  4. JavaScript is written always inside script tag. Where script is a paired tag so it also has a closing tag. Now inside these script tag, we write our JavaScript code.
  5. As here we see that our JavaScript function will run on click on the button and result is printed down in the paragraph tag.
  6. Here, inside our script tag. As you see that we store the id of our paragraph to inside a variable named result to show the required output and we store date into a variable name as date.
  7. At next step, we create our function that gets called on click on button. Here our date gets checked. As we see that check our date using if condition.
  8. Here, if the date is valid that it will displays a message having text Given date is valid otherwise if it is invalid and does not matches with our requirement then another message with text Given date is invalid displays on screen. We hope you understand how to validate a date in JavaScript.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to apply validation on date in JavaScript.

I hope this tutorial on date validation in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.