最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Comma separated step for number input box - Stack Overflow

programmeradmin2浏览0评论

I can specify the 'step' for thousand increments when a user inputs a number. But why I cannot write '1,000' instead of '1000' for the increment to happen with the ma separation for thousands? How to do this?

<div class="input"><label for="salary">Salary</label>
<input class='inp_cont' id="salary" name="salary" placeholder="Enter your salary" step="1000" min="0" required="" type="number"></div>

I can specify the 'step' for thousand increments when a user inputs a number. But why I cannot write '1,000' instead of '1000' for the increment to happen with the ma separation for thousands? How to do this?

<div class="input"><label for="salary">Salary</label>
<input class='inp_cont' id="salary" name="salary" placeholder="Enter your salary" step="1000" min="0" required="" type="number"></div>

Share Improve this question asked Oct 1, 2017 at 19:34 user4752928user4752928
Add a ment  | 

1 Answer 1

Reset to default 5

It cannot be done if you want to stick to the type="number", because:

  • As Chrome reports:

    The value must match to the following regular expression: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?

  • As specification mentions:

    value = floating-point number

Follow the above link to see that spec doesn't mention use of ma at all.


If you want to switch to type="text":

document.getElementById('salary').addEventListener('input', event =>
  event.target.value = (parseInt(event.target.value.replace(/[^\d]+/gi, '')) || 0).toLocaleString('en-US')
);
<div class="input">
  <label for="salary">Salary</label>
  <input class='inp_cont' id="salary" pattern="^[\d,]+$" name="salary" placeholder="Enter your salary" required="" type="text">
</div>

发布评论

评论列表(0)

  1. 暂无评论