I am trying to validate formate . but I am getting true value .
why ?
var a = "11-01-2022";
console.log("sss", moment(a, "YYYY-mm-DD").isValid());
Expected output is : false. here I am getting true why ?
var a = "2022-01-11";
console.log("sss", moment(a, "YYYY-mm-DD").isValid());
Expected output is : true.
here is my code https://codesandbox.io/s/patient-cookies-npsit?file=/src/App.js:85-165
You need to enable strict parsing. It's the third argument in the moment function.
console.log("sss", moment(a, "YYYY-mm-DD", true).isValid()); //Outputs false
Strict parsing requires that the format and input match exactly, including delimiters. Strict parsing is frequently the best parsing option.
Kindly pass third parameter for parsing.
console.log("sss", moment(a, "YYYY-mm-DD", true).isValid());