适用于多种格式的 jQuery zip 屏蔽

发布于 2024-09-06 13:46:44 字数 444 浏览 3 评论 0 原文

我需要屏蔽 zip 字段,以便它允许经典的 5 位 zip (XXXXX) 或 5 + 4 格式 (XXXXX-XXXX)。

我可以这样做:

$('#myZipField').mask("?99999-9999");

但复杂的是,如果用户只输入 5 位数字,则不应该显示破折号。

这是迄今为止我想出的最好的方法 - 我可以将其扩展为在插入第 6 位数字时自动插入破折号,但这样做的问题是删除时的有趣行为(我可以阻止他们删除破折号,但它会修补补丁等等,这变成了一场噩梦):

$.mask.definitions['~']='[-]';
$("#myZipField").mask("?99999~9999", {placeholder:""});

是否有任何开箱即用的方法可以做到这一点,或者我必须自己推出?

I have a requirements for masking a zip field so that it allows the classic 5 digits zip (XXXXX) or 5 + 4 format (XXXXX-XXXX).

I could so something like:

$('#myZipField').mask("?99999-9999");

but the complication comes from the fact that dash should not be showing if the user puts in only 5 digits.

This is the best I came up with so far - I could extend it to auto-insert the dash when they insert the 6th digit but the problem with this would be funny behavior on deletion (I could stop them from deleting the dash but it would patching the patch and so forth, it becomes a nightmare):

$.mask.definitions['~']='[-]';
$("#myZipField").mask("?99999~9999", {placeholder:""});

Is there any out of the box way of doing this or do I have to roll my own?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谁与争疯 2024-09-13 13:46:45

这个邮政编码实际上很简单,但是当您要处理更复杂的格式时,以下是使用插件解决它的方法(来自 演示页面):

var options =  {onKeyPress: function(cep, e, field, options){
  var masks = ['00000-000', '0-00-00-00'];
    mask = (cep.length>7) ? masks[1] : masks[0];
  $('.crazy_cep').mask(mask, options);
}};

$('.crazy_cep').mask('00000-000', options);

This zip code is actually simple, but when you have a more complex format to handle, here is how it's solved with the plugin (from the demo page):

var options =  {onKeyPress: function(cep, e, field, options){
  var masks = ['00000-000', '0-00-00-00'];
    mask = (cep.length>7) ? masks[1] : masks[0];
  $('.crazy_cep').mask(mask, options);
}};

$('.crazy_cep').mask('00000-000', options);
凡间太子 2024-09-13 13:46:45

如果您已经在使用 jQuery,可能有数百个用于遮罩等的插件,例如:
http://www.meiocodigo.com/projects/meiomask/

所以我不认为你必须自己推出

If you're using jQuery already, there are probably hundreds of plugins for masks etc, for example:
http://www.meiocodigo.com/projects/meiomask/

So I don't think you'd have to roll your own

烂柯人 2024-09-13 13:46:45

当您使用 jQuery Inputmask 插件并且想要使用 4 或 5 位数字值作为邮政编码时,您应该使用:

$('#myZipField').inputmask("9999[9]");

When you use jQuery Inputmask plugin and you want to use 4 or 5 digit values for zip code you should use:

$('#myZipField').inputmask("9999[9]");

转身以后 2024-09-13 13:46:45

为什么不让该字段透明,并在其后面放置一个文本对象,并将表单设置为浅灰色?因此,他们在背景中看到#######-####,然后对其进行配置,以便字母在键入时消失。此时,如果他们想输入额外的四个,就应该输入破折号,对吧?然后,如果脚本搞砸并输入 6 个数字,您可以操纵脚本自动插入连字符吗?

Why not have the field be transparent, and have a text object behind it with the form in light grey? So they see #######-#### in the background, and then rig it so the letters dissapear as they type. At that point, it suggests that they should enter a dash if they want to put the extra four, right? Then, you could just rig the script to autoinsert the hyphen if they mess up and type 6 numbers?

泼猴你往哪里跑 2024-09-13 13:46:44

您不必使用不同的插件。只需移动问号,这样

$('#myZipField').mask("?99999-9999");

您就应该使用:

$('#myZipField').mask("99999?-9999");

而不是:毕竟,可选的不是整个字符串,而是 - 及以后的字符串。

You don't have to use a different plug-in. Just move the question mark, so that instead of:

$('#myZipField').mask("?99999-9999");

you should use:

$('#myZipField').mask("99999?-9999");

After all, it isn't the entire string which is optional, just the - and onward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文