简体   繁体   English

使用掩码文本输入来验证MM / YY jquery-mask-plugin

[英]Mask Text Input to Validate MM/YY jquery-mask-plugin

Using jquery-mask-plugin I want to mask an input to only accept reasonable MM/YY 我想使用jquery-mask-plugin屏蔽输入以仅接受合理的MM / YY

$('#expDate').mask("99/99"); works but it allows for 55/66 which is not an acceptable month. 可以,但是它允许55/66 ,这是不可接受的月份。

How can I make it so the first part of the mask is <= 12 我该如何做,所以面罩的第一部分是<= 12

You cannot do this with just the jquery-mask plugin. 您不能仅使用jquery-mask插件来执行此操作。 As you noted, it only masks out certain TYPES of inputs, such as numbers (in general), letters, symbols, etc. It does not perform data validation (which is what you're asking for. 如您所述,它仅掩盖某些类型的输入,例如数字(通常是数字),字母,符号等。它不执行数据验证(这就是您要的内容)。

You can write a simple jQuery function that runs onblur, etc that rejects invalid combinations: 您可以编写一个运行onblur等的简单jQuery函数,以拒绝无效的组合:

$('#expDate').blur(function() {
  let val = $(this).val();
  if (val.indexOf('/') > 0)
    {
      let first = val.split('/')[0]; 
      if (first > 12)  $(this).val('');
    }   
});

Alternatively, you can use the great jQuery Validate plugin: https://jqueryvalidation.org/ 另外,您可以使用很棒的jQuery Validate插件: https : //jqueryvalidation.org/

To be clear, you want data validation beyond just basic input masking. 明确地说,您希望数据验证不只是基本的输入掩码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM