[英]inputmask dynamic mask change on last digit
I want to useJquery.Inputmask to mask a phone number input and I'm dong it as follows:我想使用Jquery.Inputmask来屏蔽电话号码输入,我将其输入如下:
@Html.TextBoxFor(m => m.PhoneNumber, new {@class = "form-control mask-phone"})
and in my JS I initialize it:并在我的 JS 中初始化它:
$(".mask-phone").inputmask({
mask: "(99)9{4,5}-9999}",
greedy: false
});
and it works great, but the dynamic input is triggered right after the sixth digit and i have to manually put the caret after the hyphen.它工作得很好,但是在第六位之后立即触发动态输入,我必须手动将插入符号放在连字符之后。 Is there a way to only trigger the dynamics in the mask after the very last digit has been typed?有没有办法只在输入最后一个数字后触发掩码中的动态?
I'm not sure I made myself clear.我不确定我是否说清楚了。 I short terms, I want a mask that goes from:我简短地说,我想要一个面具,它来自:
(99)9999-9999
to:到:
(99)99999-9999
without having to drag the caret after typing the sixth digit.键入第六位数字后无需拖动插入符号。
4 years after: the solution for this case is now in the Input mask documentation, under ' keepStatic ' option. 4 年后:此案例的解决方案现在位于“输入掩码”文档中的“ keepStatic ”选项下。 So in this case you could do somthing like this:所以在这种情况下,你可以做这样的事情:
$(".mask-phone").inputmask({
mask: ["(99)9{4}-9999", "(99)9{5}-9999"],
keepStatic: true
});
This is what I ended up using, it works pretty well这是我最终使用的,效果很好
$(".mask-telefone").on("keydown change", function() {
if ($(this).val().replace(/[_\-()]/g,"").length >= 10){
$(this).inputmask("(99)99999-9999");
else {
$(this).inputmask("(99)9999-9999");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.