html5货币/货币输入 [英] html5 input for money/currency

查看:329
本文介绍了html5货币/货币输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法计算出用于接受表格上货币价值的东西.

I seem unable to work out what to use for accepting monetary values on a form.

我尝试过...

<input type="number" min="0" max="10000" step="1" name="Broker_Fees" id="broker_fees" required="required">

但是那将不允许便士进入.

But that then won't allow for pence entries.

我希望增量按钮控件以磅为单位上升,但仍然希望能够输入便士.

I want the incremental button control to go up in pounds, but still want the ability to enter pence.

谁想使用一次移动1p的增量按钮?

Who would want to use an incremental button that moved 1p at a time?

也许我使用了错误的控件,但是找不到货币/货币控件?

Perhaps I'm using the wrong control , but I can't find a money/currency control?

有人可以建议使用HTML5接受货币值(包括逗号,小数位和货币符号)的最佳方法吗?

Can someone please advise the best way to accept monetary values (including commas, decimal places and currency symbol) using HTML5?

谢谢, 1DMF

推荐答案

为数字输入启用分数/分/小数

为了允许在HTML5数字输入上使用小数(分),您需要将"step"属性指定为="any":

In order to allow fractions (cents) on an HTML5 number input, you need to specify the "step" attribute to = "any":

<input type="number" min="1" step="any" />

当输入十进制/小数货币时,这将特别防止Chrome显示错误. Mozilla,IE等...如果忘记指定step="any",也不会出错. W3C规范指出,确实需要step ="any"来允许小数.因此,您绝对应该使用它.

This will specifically keep Chrome from displaying an error when a decimal/fractional currency is entered into the input. Mozilla, IE, etc... don't error out if you forget to specify step="any". W3C spec states that step="any" should, indeed, be needed to allow for decimals. So, you should definitely use it.

此外,数字输入现在得到了广泛的支持(> 90%的用户).

Also, the number input is now pretty widely supported (>90% of users).

货币/货币有哪些输入选项?

此后,问题的标题已经更改,并且含义略有不同.为了接受金钱/小数,可以同时使用数字或文本输入.

The title of the question has since changed and takes on a slightly different meaning. One could use both number or text input in order to accept money/decimals.

对于货币/货币输入字段,建议使用数字输入类型并指定上述适当的属性.截至2020年,还没有针对货币或金钱的实际输入类型的W3C规范.

For an input field for currency/money, it is recommended to use input type of number and specify appropriate attributes as outlined above. As of 2020, there is not a W3C spec for an actual input type of currency or money.

主要原因是它会自动强制用户输入有效的标准货币格式,并且禁止使用任何字母数字文本.话虽如此,您当然可以使用常规文本输入并进行一些后期处理,以仅获取数字/十进制值(有时还应在服务器端进行验证).

Main reason being it automatically coerces the users into entering a valid standard currency format and disallows any alphanumeric text. With that said, you could certainly use the regular text input and do some post processing to only grab the numeric/decimal value (there should be server side validation on this at some point as well).

OP详细说明了货币符号和逗号的要求.如果您想要这样更高级的逻辑/格式(从2020年开始),则需要为文本输入创建自定义JS逻辑或找到一个插件.

The OP detailed a requirement of currency symbols and commas. If you want fancier logic/formatting like that, (as of 2020) you'll need to create custom JS logic for a text input or find a plugin.

这篇关于html5货币/货币输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆