1

我使用以下选项以 LLLL 格式在 datetimepicker 中显示时间

$('#meeting_datetime').datetimepicker({defaultDate: tomorrowsDate, stepping: 5, format: 'LLLL'});

一切正常,但现在我想验证该字段,我认为它应该可以正常工作

$('#manage_jc_settings_form')
        .formValidation({
            framework: 'bootstrap',
            excluded: ':disabled',
            ignore: [],
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                meeting_datetime: {
                    validators: {
                        notEmpty: {
                            message: 'The date is required'
                        },
                        date: {
                            format: 'LLLL',
                            message: 'The date is not valid'
                        },
                        callback: {
                            message: "The next meeting can't be in the past",
                            callback: function (value, validator) {
                                var m = new moment(value, 'LLLL', true);
                                return (m > todaysDate);
                            }
                        }
                    }
                }
            }
        }
    });

但这样日期时间字段不会验证。有人知道如何在验证中使用 LLLL 格式吗?我正在使用 formValidation 插件http://formvalidation.io/ 感谢 carl

4

2 回答 2

1

日期验证类型不支持该类型的格式。您需要使用带有时刻的回调验证类型。但是,由于您已经有了回调类型并且需要返回不同的消息,因此您需要使用动态消息。

http://formvalidation.io/validators/callback/#dynamic-message-example

function(value, validator, $field) {
    // ... Do your logic checking
    if (...) {
        return {
            valid: true,    // or false
            message: 'The error message'
        }
    }

    return {
        valid: false,       // or true
        message: 'Other error message'
    }
}

然后检查日期的有效性并为此返回一条消息。并检查日期是否大于 todaysDate 并为此返回不同的消息。

于 2016-07-19T22:28:42.267 回答
0

如果 todaysDate 不是时刻,则创建它:

todaysMomentDate = moment(todaysDate);

然后比较它们:

var diffDuration = moment.duration(m.diff(todaysMomentDate);
return diffDuration.asMilliseconds() < 0;
于 2016-07-19T20:51:28.857 回答