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

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

How to add space between every 2 number?

问题

Sure, here's the translated code portion:

const input = document.getElementById("phone") || document.getElementById("secondnumber") || document.getElementById("secondnumber");

input.addEventListener("input", () => input.value = formatNumber(input.value.replaceAll(" ", "")));

const formatNumber = (number) => number.split("").reduce((seed, next, index) => {
    if (index !== 0 && !(index % 2)) seed += " ";
    return seed + next;
}, "");

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!

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

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

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

答案1

得分: 0

Sure, here's the translated code snippet:

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 -->

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;

<input type="tel" class="phone" placeholder="电话" id="phone">
<input type="tel" class="phone" placeholder="第二号码" id="secondnumber">
<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 -->

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

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

&lt;input type=&quot;tel&quot; class=&quot;phone&quot; placeholder=&quot;Phone&quot; id=&quot;phone&quot;&gt;
&lt;input type=&quot;tel&quot; class=&quot;phone&quot; placeholder=&quot;Second Number&quot; id=&quot;secondnumber&quot;&gt;
&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:

确定