用于格式化输入的 jQuery 插件
我想接受以下任何格式的值:
- -$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是我将如何实现验证规则:
您的模式的问题是它不是固定长度的,因此很难在掩码中进行编码,并且您可能会遇到一些人给出 3.5 美元,这不是您想要的。 有了你这样的模式,我想很难不依靠正则表达式匹配。
您可以考虑将分部分设为强制,在这种情况下您的模式几乎没问题,只需在末尾添加 .99 就可以了(尽管作为用户,我讨厌必须以空格字符开始我的货币.. .)。
Here is how i would implement the validation rule :
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...).
只是为了澄清安东尼的回应,他使用的插件是 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
我想这会对你有帮助。
使用语法
您可以显示符号,但最好隐藏它。
I think this will help you.
Use the syntax
You can show the symbol but it is probably better to hide it.
我知道在 mask 中你也可以将一些 mask 设为可选,所以你也许可以摆脱这个
I know that in mask you can also make some of the mask optional, so you might be able to get away with this
$(".currency").mask("~$9?.99");
是不是不行???
$(".currency").mask("~$9?.99");
is it not work???