Javascript validate date string | regular expression, format, parse methods

Using Date.parse() method you can validate the date string in Javascript. the parse() method parses a string representation of a date and returns the number of milliseconds. If the given string doesn’t represent a valid date, NaN is returned.

Examples of JavaScript validate date string

Simple date validation in JavaScript using parse method:-

Output:

Simple date validation in javascript

Another Example and Improved function that uses only Date.parse():

Output:

Javascript validate date string

Date validation in javascript using regular expression

Let’s see the example of how to validate date format in javascript using regex.

Example of JavaScript validate date format dd-mmm-yyyy

Output:

javascript validate date format dd-mmm-yyyy using regular expression

Javascript validate date format dd-mm-yyyy

Current date validation in javascript

Date validation is needed when you want to restrict the user to provide a future date.

Output:

Current date validation in javascript

Q: How to date range validation in javascript?

Answer: Checks to ensure that the values entered are dates and /are of a valid range. By this, the dates must be no more than the built-in number of days apart.

the multiplier for 86400000 should be whatever the range of days you are looking for is. So if you are looking for dates that are at least one week apart then it should be seven.

Complete code for date range check:-

Output:

date range validation in javascript

Do comment if you have any questions and suggestions on this topic.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.