屏蔽时间的代码(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)。
最佳答案
您不应使用alias
和mask
,因为您不是在声明alias
而是仅在inputmask()
中调用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
}
}
})
关于javascript - JQUERY inputmask是否可以为日期时间添加字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41757660/