All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Date Validation In JavaScript

Last Updated : Jul 1, 2023

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.

  JavaScript Validation
  Talkers Code - - JavaScript Validation
  Date validation in JavaScript
 <button onclick = "DateValidation()">
  Click Here to check date
 <p id = "result">
// JavaScript Code here
  var result = document.getElementById('result');
  var date = new Date(" provide date here ");
  function DateValidation() {
   if (
         === "[object Date]")
    if (isNaN(date.getTime())) {
     result.innerHTML = "Given date is invalid date .";
    else {
     result.innerHTML = " Given date is a valid date.";
  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.