1

屏蔽时间的代码 (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 inputmask 库(https://github.com/RobinHerbots/jquery.inputmask)。

4

1 回答 1

2

您不应该使用aliasand ,mask因为您没有声明 aalias而只是调用inputmask()with options

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

$('.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
        }
    }
})
于 2017-01-25T03:04:49.143 回答