如何使用掩码插件验证美国邮政编码?

How to validate the us zipcode using mask plugin?

我不想让邮政编码的首字母全为零。

$('#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>

动态蒙版应该有帮助:

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);
<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>