javascript jQuery inputmask 允许保留占位符文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20714278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:05:00  来源:igfitidea点击:

jQuery inputmask allowing placeholder text to remain

javascriptjquery

提问by Jason

I am using the inputmask from https://github.com/RobinHerbots/jquery.inputmaskand applying the mask to all textboxes with class "date". The problem I am having is that if the user leaves one or more of the letters from the placeholder in place, it will be accepted. For example, the user enter 10 but then clicks out of the field and the field is left with 10/dd/yyyy. How can I stop this behavior? I want to prevent the user from leaving the field when it looks like this.

我正在使用来自https://github.com/RobinHerbots/jquery.inputmask 的输入掩码并将掩码应用于所有具有“日期”类的文本框。我遇到的问题是,如果用户将占位符中的一个或多个字母留在原地,它将被接受。例如,用户输入 10,然后单击该字段外,该字段留下 10/dd/yyyy。我怎样才能阻止这种行为?当它看起来像这样时,我想防止用户离开该字段。

here is the jQuery for the mask:

这是面具的jQuery:

$('.date').inputmask("mm/dd/yyyy", { "placeholder": "mm/dd/yyyy" })

回答by Juan

you could use the onincompleteevent to clear the input when ever is not complete

您可以使用该onincomplete事件在未完成时清除输入

$('.date').inputmask("mm/dd/yyyy", {
  "placeholder": "mm/dd/yyyy",
  onincomplete: function() {
    $(this).val('');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://rawgithub.com/RobinHerbots/jquery.inputmask/2.x/dist/jquery.inputmask.bundle.js"></script>

<input type="text" class="date" />