Correct way to handle invalid date
When allowing manual input editing in flatpickr, it is easy to accidentally enter an invalid date. What is the correct way to handle it?
I included a parser in my initialisation as follows:
let fp = flatpickr(id, { altInput: true ,dateFormat: 'Y-m-d' ,altFormat: 'd/m/Y' ,allowInput: true ,allowInvalidPreload: true ,parseDate: (datestr, format) => { let parsed_date = moment(datestr, 'DD MM YYYY', false); if(!parsed_date.isValid()) { let error = 'unable to parse "'+datestr+'"<br>Invalid at '+parsed_date.invalidAt(); alert(error); return false; } return parsed_date.toDate(); } });
This works fine until you enter an invalid date (e.g. 31/4/2026) at which point it gets into a loop as any clicking on the document seems to fire the parser again and I get a stream of invalid date errors thrown.
How should I be handling the error cleanly?
Thanks
Norman B
Source: flatpickr/flatpickr