moment.js isValid function not working properly

In your question you write that moment('03:55jojojo', 'HH:mm',true).isValid(); returns true. This is incorrect. Please check your jsfiddle again.

From http://momentjs.com/docs/

Moment’s parser is very forgiving, and this can lead to undesired
behavior. As of version 2.3.0, you may specify a boolean for the last
argument to make Moment use strict parsing. Strict parsing requires
that the format and input match exactly.

moment('It is 2012-05-25', 'YYYY-MM-DD').isValid();        // true
moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid();  // false
moment('2012-05-25', 'YYYY-MM-DD', true).isValid();        // true

You can use both language and strictness.

moment('2012-10-14', 'YYYY-MM-DD', 'fr', true);

Leave a Comment