如何在HTML表单中创建美元金额输入字段? [英] How to create a dollar amount input field in an HTML form?

查看:407
本文介绍了如何在HTML表单中创建美元金额输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我创建的HTML表单,它有一个需要花费一定金额的字段。



从我读过的内容来看,JavaScript没有将小数识别为数字组成部分,并且公认的做法是以美分输入而不是处理美元数额。

问题是,我正在写的表单提交给一个读取表单输入并执行一些操作的函数,并将这个美元金额作为文本格式# #。##并且会拒绝任何不是这种确切格式的输入。



我想知道的是:是否可以创建一个包含5个字符并将第三个字符设置为永久小数点的文本框?

如果没有,是否有任何方法可以使用JS验证来确保输入格式正确?



谢谢!

解决方案

我认为您将不得不验证它。这篇文章对验证金额有一些很好的正则表达式:货币验证。它也有一些好的点如何修剪,以便用户不能输入$ 412.234或类似的东西。

I have an HTML form I've created that has a field that needs to take a dollar amount.

From what I've read, JavaScript doesn't recognize decimals as numeric components and the accepted practice is to take input in cents rather than deal with dollar amounts.

Problem is, the form I'm writing submits to a function that reads the form input and performs some actions with them and takes this dollar amount as text in the format ##.## and will reject any input not in this exact format.

What I'm wondering is: is it possible to create a text box that has takes 5 characters and sets the third character as a permanent decimal point?

If not, is there any way I can use JS validation to ensure that the input is in the proper format?

Thanks!

解决方案

I think you are going to have to validate it. This post has some good regex for validation dollar amounts: Currency validation. It also has some good points for how to trim so that a user can't enter $412.234 or anything like that.

这篇关于如何在HTML表单中创建美元金额输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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