-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Date validation failure. #2458
Comments
I believe that you used the |
I'm using the DateISO method |
@lgnl272 Please provide a jsfiddle link |
Duplicate bug: #276 |
I find it interesting that the 'date' validator is deprecated in favor for date ISO which is a standard notation/format. Probably need to implement ISO week date which is part of ISO 8061 so the date can be checked for two(2) things:
DateISO should be named DateISO8061 or DateISOFormat, then de-depricate the 'date' validator. Date validator correctly errors on new Date('1983-02-30') as it should. |
Reminds me of an old pull request: #2032 |
adding the !/Invalid|NaN/.test( new Date( value ) check fixes the current use case. Screen shot of test cases: |
new Date (dateString) format includes date-only forms:
YYYY-MM-DD follows exactly as a Date only ISO format (not including time). I'll add a push and create a pull request. |
Disregard the above, I now see the problem with Date() and how different browsers interpret. |
ChatGPT came up with this code, and it works for me in Firefox, Safari and Chrome. /**
* Checks if a given string is a valid date in the format MM/DD/YYYY.
* @param {string} dateString - The string to validate.
* @returns {boolean} True if the string is a valid date, false otherwise.
*/
function isValidDate(dateString) {
const dateFormatRegex = /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/;
// Check if the string matches the expected date format.
if (!dateString.match(dateFormatRegex)) {
// The string does not match the expected format.
console.log( "The string does not match the expected format.");
return false;
}
// Split the string into its component parts.
const [year, month, day] = dateString.split('-').map(part => parseInt(part));
// Check if the month is valid.
if (month < 1 || month > 12) {
console.log( "The month is not valid.");
return false;
}
// Check if the day is valid for the given month and year.
const daysInMonth = getDaysInMonth(month, year);
if (day < 1 || day > daysInMonth) {
console.log( "The day is not valid.");
return false;
}
// The string is a valid date.
return true;
}
/**
* Returns the number of days in the given month for the given year.
* @param {number} month - The month (1-12).
* @param {number} year - The year.
* @returns {number} The number of days in the given month for the given year.
*/
function getDaysInMonth(month, year) {
if (month === 2 && isLeapYear(year)) {
return 29;
}
return [31,28,31,30,31,30,31,31,30,31,30,31][month - 1];
}
/**
* Checks if the given year is a leap year.
* @param {number} year - The year.
* @returns {boolean} True if the year is a leap year, false otherwise.
*/
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
} |
Here is a jsfiddle: JS Fiddle 'isValidDate' I have added a test case, grunt completes successful, all tests pass. Questions: I think this solution will close a lot of date issues. |
Your environment
Current behavior
Validator allows any kind of dates even considerating day 30 in February
Expected behavior
Validator must return an error message.
Live demo
Try to validate 1983-02-30 and validator doesn't show anny error message
The text was updated successfully, but these errors were encountered: