JQUERY inputmask 是否可以为日期时间添加字符串?

Is JQUERY inputmask can add string for datetime?

屏蔽时间的代码(HH:mm:ss)

$('.time').each(function () { 
        $(this).inputmask({
                mask: "h:s:s",
                placeholder: "00:00:00",
                alias: "datetime",
                hourFormat: "24"
        }); 
});

如何屏蔽也接受像 NONE 这样的字符串的日期时间?可以输入文本,但只有 None 值有效 NONE 或 01:00:00

我正在使用 jquery 输入掩码库 (https://github.com/RobinHerbots/jquery.inputmask)。

你不应该使用 aliasmask 因为你不是在声明 alias 而是简单地用 options.[=17= 调用 inputmask() ]

这是带有注释解释的解决方案。

$('.time').inputmask({
    mask:'(h:s:s)|(X)', // X will define our new validator , | mean OR 
    definitions: {
        "X": {
            // Needed since you need the word(NONE) comparaison
            validator: function(chrs) {return chrs.toUpperCase().localeCompare('NONE')==0 },
            cardinality: 4, // Mean 1 instance of our new validator(X) has a value of 4 chars
            prevalidator: [
                {validator: function(chrs){return chrs.toUpperCase().localeCompare('N')==0 },cardinality:1},
                {validator: function(chrs){return chrs.toUpperCase().localeCompare('NO')==0 },cardinality:2},
                {validator: function(chrs){return chrs.toUpperCase().localeCompare('NON')==0 },cardinality:3}
            ],
            casing: "upper" // All chars will be casted as UPPER but not during our custom validation
        }
    }
})

里面有死亡金属:)

$('.time').inputmask({
    mask:'(h:s:s)|(X)', // X will define our new validator , | mean OR 
    regex: Inputmask().opts.aliases.datetime.regex, //Needed for the imported validator
    placeholder: "00:00:00", //To get 00:00:00 in place __:__:__
    hourFormat: "24", // or 12
    definitions: {
        "h": Inputmask().opts.aliases.datetime.definitions.h, // first char > 2 will become 09:
        "s": Inputmask().opts.aliases.datetime.definitions.s, //first char for minute/second if > 5 become 09
        "X": {
            // Needed since you need the word(NONE) comparaison
            validator: function(chrs) {return chrs.toUpperCase().localeCompare('NONE')==0 },
            cardinality: 4, // Mean 1 instance of our new validator(X) has a value of 4 chars
            prevalidator: [
                {validator: function(chrs){return chrs.toUpperCase().localeCompare('N')==0 },cardinality:1},
                {validator: function(chrs){return chrs.toUpperCase().localeCompare('NO')==0 },cardinality:2},
                {validator: function(chrs){return chrs.toUpperCase().localeCompare('NON')==0 },cardinality:3}
            ],
            casing: "upper" // All chars will be casted as UPPER but not during our custom validation
        }
    }
})