Date Validation
Validate date inputs
BeginnerTopic: Date/Time Programs
JavaScript Date Validation Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Check if valid date
function isValidDate(date) {
return date instanceof Date && !isNaN(date.getTime());
}
console.log('Valid:', isValidDate(new Date()));
console.log('Invalid:', isValidDate(new Date('invalid')));
// Method 2: Validate date string
function isValidDateString(dateString) {
const date = new Date(dateString);
return isValidDate(date) && dateString !== 'Invalid Date';
}
console.log('Valid string:', isValidDateString('2024-01-15'));
console.log('Invalid string:', isValidDateString('not-a-date'));
// Method 3: Validate date format
function isValidDateFormat(dateString, format = 'YYYY-MM-DD') {
const patterns = {
'YYYY-MM-DD': /^\d{4}-\d{2}-\d{2}$/,
'DD/MM/YYYY': /^\d{2}\/\d{2}\/\d{4}$/,
'MM/DD/YYYY': /^\d{2}\/\d{2}\/\d{4}$/
};
if (!patterns[format].test(dateString)) {
return false;
}
const date = new Date(dateString);
return isValidDate(date);
}
console.log('Valid format:', isValidDateFormat('2024-01-15', 'YYYY-MM-DD'));
// Method 4: Validate date range
function isDateInRange(date, minDate, maxDate) {
if (!isValidDate(date)) return false;
return date >= minDate && date <= maxDate;
}
const checkDate = new Date('2024-01-15');
const min = new Date('2024-01-01');
const max = new Date('2024-12-31');
console.log('In range:', isDateInRange(checkDate, min, max));
// Method 5: Validate future date
function isFutureDate(date) {
if (!isValidDate(date)) return false;
return date > new Date();
}
console.log('Future date:', isFutureDate(new Date('2025-01-01')));
// Method 6: Validate past date
function isPastDate(date) {
if (!isValidDate(date)) return false;
return date < new Date();
}
console.log('Past date:', isPastDate(new Date('2020-01-01')));
// Method 7: Validate date components
function validateDateComponents(year, month, day) {
if (year < 1900 || year > 2100) return false;
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
const date = new Date(year, month - 1, day);
return date.getFullYear() === year &&
date.getMonth() === month - 1 &&
date.getDate() === day;
}
console.log('Valid components:', validateDateComponents(2024, 1, 15));
console.log('Invalid components:', validateDateComponents(2024, 2, 30)); // Feb 30 doesn't existOutput
Valid: true Invalid: false Valid string: true Invalid string: false Valid format: true In range: true Future date: true Past date: true Valid components: true Invalid components: false
Understanding Date Validation
Date validation ensures correct dates.
Validation Methods
•Check Date object validity
•Validate date strings
•Check format patterns
•Validate components
Common Checks
•Valid date object
•Correct format
•In range
•Future/past
Component Validation
•Year range
•Month (1-12)
•Day (1-31)
•Handle leap years
Use Cases
•Form validation
•Input sanitization
•Data integrity
•Error prevention
Best Practices
•Validate early
•Provide clear errors
•Handle edge cases
•Test thoroughly
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.