在每两个数字之间如何添加空格?

huangapple go评论86阅读模式
英文:

How to add space between every 2 number?

问题

Sure, here's the translated code portion:

  1. const input = document.getElementById("phone") || document.getElementById("secondnumber") || document.getElementById("secondnumber");
  2. input.addEventListener("input", () => input.value = formatNumber(input.value.replaceAll(" ", "")));
  3. const formatNumber = (number) => number.split("").reduce((seed, next, index) => {
  4. if (index !== 0 && !(index % 2)) seed += " ";
  5. return seed + next;
  6. }, "");

If you have any more questions or need further assistance, please feel free to ask.

英文:

I have 3 input field,when i write in the field, how to add space between every 2 number using the ID of the input, in javascript?
Any help please!

  1. <input type="number" class="phone" placeholder="Phone" id="phone" />
  2. <input type="number" class="phone" placeholder="Second Number" id="secondnumber" />
  3. <input type="number" class="phone" placeholder="Fax" id="fax" />

i'm trying this script, but it's not apply for all the input

  1. const input = document.getElementById("phone") || document.getElementById("secondnumber") || document.getElementById("secondnumber");
  2. input.addEventListener("input", () => input.value = formatNumber(input.value.replaceAll(" ", "")));
  3. const formatNumber = (number) => number.split("").reduce((seed, next, index) => {
  4. if (index !== 0 && !(index % 2)) seed += " ";
  5. return seed + next;
  6. }, "");

答案1

得分: 0

Sure, here's the translated code snippet:

  1. console.log(("123456789abcdefgh".match(/.{1,2}/g) || []).join(" "))

Please let me know if you need any further assistance.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. console.log((&quot;123456789abcdefgh&quot;.match(/.{1,2}/g) || []).join(&quot; &quot;))

<!-- end snippet -->

答案2

得分: 0

你无法在&lt;input type=&quot;number&quot;&gt;中添加空格。根据你的示例,更合适的是指定type=&quot;tel&quot;

  1. <input type="tel" class="phone" placeholder="电话" id="phone">
  2. <input type="tel" class="phone" placeholder="第二号码" id="secondnumber">
  3. <input type="tel" class="phone" placeholder="传真" id="fax">

你提供的代码片段中已经使用了type=&quot;tel&quot;来定义电话号码输入字段。

英文:

You will not be able to add a space in &lt;input type=&quot;number&quot;&gt;. Judging by your example, it would be more appropriate to specify type=&quot;tel&quot;:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. document.querySelectorAll(&#39;.phone&#39;).forEach(el =&gt; {
  2. el.oninput = () =&gt; el.value = el.value.replace(/\D/g, &#39;&#39;).replace(/(.{2})/g, &#39;$1 &#39;);
  3. })

<!-- language: lang-html -->

  1. &lt;input type=&quot;tel&quot; class=&quot;phone&quot; placeholder=&quot;Phone&quot; id=&quot;phone&quot;&gt;
  2. &lt;input type=&quot;tel&quot; class=&quot;phone&quot; placeholder=&quot;Second Number&quot; id=&quot;secondnumber&quot;&gt;
  3. &lt;input type=&quot;tel&quot; class=&quot;phone&quot; placeholder=&quot;Fax&quot; id=&quot;fax&quot;&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月17日 23:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定