如何使用掩码插件验证我们的邮政编码?

时间:2017-02-23 08:28:49

标签: jquery html jquery-mask

我不想让第一个字母全部为零码。

$('#zipcode').mask("Y0000", {
  'clearIfNotMatch': true,
  'translation': {
    Y: {
      pattern: /[1-9]/
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js"></script>

<input name="zipcode" id="zipcode" class="form-control" type="text" required>

1 个答案:

答案 0 :(得分:1)

动态掩码应该有所帮助:

&#13;
&#13;
var options =  {
  'onKeyPress': function(cep, e, field, options){
    var masks = ['00000', '0000Z'];
    mask = (cep == '0000') ? masks[1] : masks[0];
    $('#zipcode').mask(mask, options);
  },
  'clearIfNotMatch': true,
  'translation': {
      'Z': {
        pattern: /[1-9]/
      }
    }
};

$('#zipcode').mask("00000", options);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js"></script>

<input name="zipcode" id="zipcode" class="form-control" type="text" required>
&#13;
&#13;
&#13;