It is very important to validate the data supplied by the user through a form before you process it. Among various kind of data validation, validation of date is one.
In this tutorial, we discussed how you can perform JavaScript date validation in
1. dd/mm/yyyy or dd-mm-yyyy format.
2. mm/dd/yyyy or mm-dd-yyyy format.
1. dd/mm/yyyy or dd-mm-yyyy format.
2. mm/dd/yyyy or mm-dd-yyyy format.
In the following examples, a JavaScript function is used to check a valid date format against a regular expression. Later we take each part of the string supplied by user (i.e. dd, mm and yyyy) and check whether dd is a valid date, mm is a valid month or yyyy is a valid year. We have also checked the leap year factor for the month of February. We have used "/" and "-" character as a separator within the date format but you are free to change that separator the way you want.
Validate dd/mm/yyyy or dd-mm-yyyy format
HTML Code
JavaScript Code
Flowchart:
CSS Code
Validate mm/dd/yyyy or mm-dd-yyyy format
HTML Code
JavaScript Code
No comments:
Post a Comment