Open In App

How to validate a Date in ReactJS?

Last Updated : 16 Dec, 2020
Like Article

Data validation is an important step in every application in order to authenticate the user’s entered data which can be used to calculate the date of birth or any other use. It can be achieved using the validator module in ReactJS. The following example shows how to validate the user entered data and check whether it is valid or not using the npm module in the ReactJS application.

Creating React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app datevalidatedemo

Step 2: After creating your project folder i.e. datevalidatedemo, move to it using the following command:

cd datevalidatedemo

Step 3: After creating the ReactJS application, Install the validator module using the following command:

npm install validator

Project Structure: It will look like the following.

Project Structure

App.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code to validate the date with basic UI.


import React, { useState } from "react";
import validator from 'validator'
const App = () => {
  const [errorMessage, setErrorMessage] = useState('')
  const validateDate = (value) => {
    if (validator.isDate(value)) {
      setErrorMessage('Valid Date :)')
    } else {
      setErrorMessage('Enter Valid Date!')
  return (
    <div style={{
      marginLeft: '200px',
        <h2>Validating Date in ReactJS</h2>
        <span>Enter Date: </span><input type="text" 
        onChange={(e) => validateDate(}></input> <br />
        <span style={{
          fontWeight: 'bold',
          color: 'red',
export default App

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start


  • The following will be the output if the user enters an invalid date as shown below:

  • The following will be the output if the user enters a valid date as shown below:

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads