Javascript 中的逗号弄乱了数字输入
我有一个页面,其中包含一些由用户控制的元素。其中之一是文本输入字段,用户应该在其中输入数字。如果用户仅输入数字(EG 9000),则一切正常,但如果用户使用逗号表示法(即 9,000),则 javascript 不会将输入视为整数。
如何删除逗号和/或强制输入为整数?我尝试使用 parseint(),但它似乎不适用于逗号。
I have a page with some elements that are controlled by the user. One of these is a text input field, where the user is supposed to input a number. Everything works well if the user only inputs digits (EG 9000), but is the user uses comma notation (the being 9,000) javascript doesn't take the input as an integer.
How can I remove the commas and/or force the input to an integer? I tried using parseint(), but it doesn't seem to work with commas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用全局正则表达式将所有逗号替换为空字符串:
Use a global regular expression to replace all commas with an empty string:
甚至更好
or even better
或者甚至更好,考虑到用户输入通常不可靠,使用它来删除所有非数字字符:
<代码>
Or even better, given the general unreliability of user input, use this to get rid of all non-numeric characters:
也许
让我们谈谈限制:
您可以/应该允许用户同时输入 9000 和 9000。 9,000
您可以通过 REGEX 检查有效性。
在服务器端 - 您应该删除逗号并将其视为整数。
maybe
lets talk about the restriction :
you can/should allow the user to enter both 9000 & 9,000
you can check validy via REGEX.
in the server side - you should eleminate the commas and treat it as integer.