user3795309
user3795309

Reputation: 136

Moment.js Date Validation

Here is some code taken from the chrome console. It looks like moment.js isn't forcing the 'year' paramater to be 4 characters even though that's what I stated in the format.

date
"16/01/14"
moment( date, 'YYYY-MM-DD').isValid()
true

In short try

moment( "16/01/14", 'YYYY-MM-DD').isValid()

I would expect this to be false, but it's true. Is there anyway to force moment to be more restrictive?

Upvotes: 1

Views: 4598

Answers (1)

Ben
Ben

Reputation: 10104

From http://momentjs.com/docs/#/parsing/ :

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( "16/01/14", 'YYYY-MM-DD', true).isValid()
> false

Upvotes: 3

Related Questions