Topic: Manually typed impossible dates pass our appointment validation
cobaltWorks82
free
asked 1 week ago
The booking form allows typing a date in DD/MM/YYYY format as well as selecting it. Our custom parser accepts values such as 31/02/2026 and JavaScript turns them into another day, so the Save button becomes enabled.
We do not want corrected dates to be accepted silently. How can we tell whether the supplied calendar day actually exists before sending it to the API?
Igor Przybysz
staff
answered 1 week ago
Parse the configured format into separate day, month and year fields, then verify that constructing that calendar date returns the same fields. JavaScript's date construction can normalize an overflowing day into the next month, so a valid Date object by itself is not sufficient validation.
The helper below checks the calendar fields without using the browser's ambiguous localized-string parser. Add your application's allowed year range and time-zone rules separately, and enforce the same input contract on the server.
function validCalendarDate(value) {
const match = /^(\d{2})\/(\d{2})\/(\d{4})$/.exec(value);
if (!match) return false;
const [, dd, mm, yyyy] = match;
const day = Number(dd), month = Number(mm), year = Number(yyyy);
const date = new Date(0);
date.setUTCFullYear(year, month - 1, day);
return date.getUTCFullYear() === year
&& date.getUTCMonth() === month - 1
&& date.getUTCDate() === day;
}
https://mdbootstrap.com/docs/standard/forms/datetimepicker/
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Answered
- ForumUser: Free
- Premium support: No
- Technology: MDB Standard
- MDB Version: MDB5 9.3.0
- Device: Laptop
- Browser: Safari
- OS: macOS
- Provided sample code: No
- Provided link: No