英文:
jquery input mask plugin add/remove based on first three digits
问题
I am using the jQuery inputmask plugin, version: 5.0.9-beta.4
.
I have a username field that should only be masked if the first 3 characters entered by the user are equal to 784
, else the mask should be removed.
var inputMsk = $('#txtUsername').inputmask('999-9999-9999999-9');
inputMsk.inputmask('remove');
$('#txtUsername').on('input', function() {
var fieldValue = $(this).val();
if (fieldValue.startsWith('784')) {
if ($('#txtUsername').inputmask) {
inputMsk = $('#txtUsername').inputmask('999-9999-9999999-9');
}
} else {
if ($('#txtUsername').inputmask) {
inputMsk.inputmask('remove');
}
}
});
The above code generates an error in the console when I enter 784
and then remove the 4
.
The error is:
Uncaught TypeError: f is not a function
The console identifies the following code from the inputmask.js
plugin file as the source of the error:
keypressEvent: function (e, t, i, a, o) {
var c = this.inputmask || this,
u = c.opts,
f = c.dependencyLib,
p = c.maskset,
d = c.el,
h = f(d),//----------on this line error is generated
m = e.key;
英文:
I am using the jQuery inputmask plugin, version: 5.0.9-beta.4
.
I have a username field that should only be masked if first 3 characters entered by the user is equal to 784
, else the mask should be removed.
var inputMsk = $('#txtUsername').inputmask('999-9999-9999999-9');
inputMsk.inputmask('remove');
$('#txtUsername').on('input', function() {
var fieldValue = $(this).val();
if (fieldValue.startsWith('784')) {
if ($('#txtUsername').inputmask) {
inputMsk = $('#txtUsername').inputmask('999-9999-9999999-9');
}
} else {
if ($('#txtUsername').inputmask) {
inputMsk.inputmask('remove');
}
}
});
The above code generates an error in the console when I a enter 784
and then remove the 4
.
The error is:
> Uncaught TypeError: f is not a function
The console identifies the following code from the inputmask.js
plugin file as the source of the error:
keypressEvent: function (e, t, i, a, o) {
var c = this.inputmask || this,
u = c.opts,
f = c.dependencyLib,
p = c.maskset,
d = c.el,
h = f(d),//----------on this line error is generated
m = e.key;
答案1
得分: 1
我不太熟悉inputmask插件的先前经验,但我在您的代码示例上工作并得出了一个似乎有效的解决方案。
首先,类型错误是由插件的代码引发的。我不确定导致这个错误的确切原因,但在我看来,它似乎与删除掩码以及触发keyup
事件处理程序的时机有关。这让我有一种预感,将.inputmask('remove')
调用包装在具有超时值为0
的setTimeout
中可能会有所帮助 - 并且确实有帮助。有关更多信息,请参见:https://stackoverflow.com/q/779379/3397771。
setTimeout
防止了错误,但它并没有清除掩码。所以我尝试用空字符串.inputmask('')
重新设置掩码,而不是调用.inputmask('remove')
。这似乎产生了所期望的效果。
const $input = $('#txtUsername');
$input.on('input', function() {
const fieldValue = $input.val();
if (fieldValue.startsWith('784')) {
$input.inputmask('999-9999-9999999-9');
} else {
setTimeout(() => {
$input.inputmask('');
}, 0);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/inputmask@5.0.8/dist/jquery.inputmask.min.js"></script>
<input id="txtUsername">
请注意,这是您提供的代码的翻译版本。
<details>
<summary>英文:</summary>
I don't have much previous experience with the [inputmask plugin][1], but I worked on your code example and I arrived at a solution that seems to work.
First, the Type Error is being thrown by the plugin's code. I don't know exactly what is causing it, but it looks to me to have something to do with the timing of when the mask is removed and the handlers for the `keyup` event are fired. This gave me a hunch that wrapping the `.inputmask('remove')` call in a `setTimeout` with a timeout value of `0` could help - and it did. For more information on this, see: https://stackoverflow.com/q/779379/3397771.
The `setTimeout` prevented the error, but it did not clear the mask. So I tried re-setting the mask with an empty string, `.inputmask('')`, instead of calling `.inputmask('remove')`. This seems to have given the desired effect.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const $input = $('#txtUsername');
$input.on('input', function() {
const fieldValue = $input.val();
if (fieldValue.startsWith('784')) {
$input.inputmask('999-9999-9999999-9');
} else {
setTimeout(() => {
$input.inputmask('');
}, 0);
}
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/inputmask@5.0.8/dist/jquery.inputmask.min.js"></script>
<input id="txtUsername">
<!-- end snippet -->
[1]: https://robinherbots.github.io/Inputmask/#/documentation
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论