I want to validate a date in a NodeJS application.
I tried the MomentsJs
library, but it seems that the isValid()
function ignored all alpha characters, it's too tolerant for me (When I specify the dateformat as
, I expect that the date is a 10-characters-string.):
moment("One,2 and 011, 12-10", "YYYY-MM-DD").isValid() // returns true instead of false moment("Seppl"); // parsing seems to work with all values?
Is there any way to check dates more strictly?
How can I check if parsing a date fails? (It returns
when printing wrong dates it).
Problem courtesy of: Sonson123
Moment.js library doesnt provide any method validate the date with given format.
moment("", dateFormat) will just check the given date but not with given format.
Following lines from momentjs.com
Note: It is not intended to be used to validate that the input string matches the format string. Because the strictness of format matching can vary depending on the application and business requirements, this sort of validation is not included in Moment.js.
Solution courtesy of: Kiran Nunna