moment.js isValid函数不能正常工作 [英] moment.js isValid function not working properly
问题描述
我有这个问题...我没有找到任何类似的东西,似乎也很奇怪,没有人会遇到这个问题,用moment.js验证时间。
I have this question... I haven't found anything similar and it also seems very strange that nobody had this problem validating time with moment.js.
moment('03:55', 'HH:mm').isValid(); //true
moment('03:55jojojo', 'HH:mm').isValid(); //true
moment('03:55jojojo', 'HH:mm',true).isValid(); //true
我做错了吗?
这是一个例子:
Am I doing something wrong? Here is an example:
http: //jsfiddle.net/vCGAp/145/
推荐答案
在你的问题中你写的是 moment('03:55jojojo','HH:mm',true).isValid();
返回true。这是不正确的请检查您的jsfiddle。
In your question you write that moment('03:55jojojo', 'HH:mm',true).isValid();
returns true. This is incorrect. Please check your jsfiddle again.
Moment的解析器非常宽恕,这可能会导致不必要的
行为。从版本2.3.0开始,您可以为最后一个
参数指定布尔值,以使Moment使用严格解析。严格的解析需要
格式和输入完全匹配。
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);
这篇关于moment.js isValid函数不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!