用于格式化输入的 jQuery 插件

发布于 2024-07-24 05:29:05 字数 363 浏览 3 评论 0原文

我想接受以下任何格式的值:

  • -$5
  • -$5.00
  • $5
  • $5.00

是否可以使用 屏蔽输入插件

如果没有,我要找什么插件?

如何指示某个字符是可选的?

到目前为止我得到的代码

$.mask.definitions['~']='[ +-]';
$(".currency").mask("~$9");

I want to accept values in any of the following format:

  • -$5
  • -$5.00
  • $5
  • $5.00

Is it possible to do that using Masked Input Plugin?

If not, what plug-in am I looking for?

How do I indicate a character is optional?

The code I've got so far

$.mask.definitions['~']='[ +-]';
$(".currency").mask("~$9");

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

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

发布评论

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

评论(5

弃爱 2024-07-31 05:29:05

以下是我将如何实现验证规则:

$('.myinput').val().match(/^[+-]?\$\d(?:\.\d\d)?$/)

您的模式的问题是它不是固定长度的,因此很难在掩码中进行编码,并且您可能会遇到一些人给出 3.5 美元,这不是您想要的。 有了你这样的模式,我想很难不依靠正则表达式匹配。

您可以考虑将分部分设为强制,在这种情况下您的模式几乎没问题,只需在末尾添加 .99 就可以了(尽管作为用户,我讨厌必须以空格字符开始我的货币.. .)。

Here is how i would implement the validation rule :

$('.myinput').val().match(/^[+-]?\$\d(?:\.\d\d)?$/)

The problem with your pattern is that it is not fixed-length, so hard to code in mask, and you may encounter some people giving $3.5, which is not what you want. With such a pattern of yours, I think it will be hard not to fall back on regexp matching.

You may consider making the cent part mandatory, in which case your pattern is almost ok, just add .99 at the end and it should do it (although as a user I would hate to have to start my currency with a space character...).

岁吢 2024-07-31 05:29:05

只是为了澄清安东尼的回应,他使用的插件是 jquery-maskmoney:

https://github.com/plentz /jquery-maskmoney

Just to clarify Antony response, the plugin that he's using is jquery-maskmoney:

https://github.com/plentz/jquery-maskmoney

彼岸花似海 2024-07-31 05:29:05

我想这会对你有帮助。

使用语法

$(mask_id).maskMoney({showSymbol:false,decimal:'.',precision:2});

您可以显示符号,但最好隐藏它。

I think this will help you.

Use the syntax

$(mask_id).maskMoney({showSymbol:false,decimal:'.',precision:2});

You can show the symbol but it is probably better to hide it.

攒眉千度 2024-07-31 05:29:05

我知道在 mask 中你也可以将一些 mask 设为可选,所以你也许可以摆脱这个

$.mask.definitions['~']='[ +-]';
$(".currency").mask("~$9?.99");

I know that in mask you can also make some of the mask optional, so you might be able to get away with this

$.mask.definitions['~']='[ +-]';
$(".currency").mask("~$9?.99");
月牙弯弯 2024-07-31 05:29:05
$.mask.definitions['~']='[ +-]';

$(".currency").mask("~$9?.99");

是不是不行???

$.mask.definitions['~']='[ +-]';

$(".currency").mask("~$9?.99");

is it not work???

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