How to Add space between every 4 characters in JavaScript? [duplicate]












-4
















This question already has an answer here:




  • How do I style numbers?

    2 answers



  • Format credit card number

    8 answers




I want to add a space between every 4 characters. I'm developing a webpage about credit card numbers.



example



var x = document.getElementById("card_number").value;


example :
if the value of x is 1234567890123456



I need to split this number every 4 characters and add a space. Like this



1234 5678 9012 3456



and I need this number on a variable. Because I need to set this number on the textbox.
Please help me. thanks










share|improve this question















marked as duplicate by mplungjan html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Simular question like: stackoverflow.com/questions/1772941/… use the search function

    – episch
    Nov 22 '18 at 8:56











  • Please show minimum effort

    – mplungjan
    Nov 22 '18 at 8:57











  • Alternative search

    – mplungjan
    Nov 22 '18 at 8:58
















-4
















This question already has an answer here:




  • How do I style numbers?

    2 answers



  • Format credit card number

    8 answers




I want to add a space between every 4 characters. I'm developing a webpage about credit card numbers.



example



var x = document.getElementById("card_number").value;


example :
if the value of x is 1234567890123456



I need to split this number every 4 characters and add a space. Like this



1234 5678 9012 3456



and I need this number on a variable. Because I need to set this number on the textbox.
Please help me. thanks










share|improve this question















marked as duplicate by mplungjan html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Simular question like: stackoverflow.com/questions/1772941/… use the search function

    – episch
    Nov 22 '18 at 8:56











  • Please show minimum effort

    – mplungjan
    Nov 22 '18 at 8:57











  • Alternative search

    – mplungjan
    Nov 22 '18 at 8:58














-4












-4








-4


0







This question already has an answer here:




  • How do I style numbers?

    2 answers



  • Format credit card number

    8 answers




I want to add a space between every 4 characters. I'm developing a webpage about credit card numbers.



example



var x = document.getElementById("card_number").value;


example :
if the value of x is 1234567890123456



I need to split this number every 4 characters and add a space. Like this



1234 5678 9012 3456



and I need this number on a variable. Because I need to set this number on the textbox.
Please help me. thanks










share|improve this question

















This question already has an answer here:




  • How do I style numbers?

    2 answers



  • Format credit card number

    8 answers




I want to add a space between every 4 characters. I'm developing a webpage about credit card numbers.



example



var x = document.getElementById("card_number").value;


example :
if the value of x is 1234567890123456



I need to split this number every 4 characters and add a space. Like this



1234 5678 9012 3456



and I need this number on a variable. Because I need to set this number on the textbox.
Please help me. thanks





This question already has an answer here:




  • How do I style numbers?

    2 answers



  • Format credit card number

    8 answers








javascript html split






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:12









Antu

791721




791721










asked Nov 22 '18 at 8:52









Chethiya_KChethiya_K

11




11




marked as duplicate by mplungjan html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by mplungjan html
Users with the  html badge can single-handedly close html questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 8:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Simular question like: stackoverflow.com/questions/1772941/… use the search function

    – episch
    Nov 22 '18 at 8:56











  • Please show minimum effort

    – mplungjan
    Nov 22 '18 at 8:57











  • Alternative search

    – mplungjan
    Nov 22 '18 at 8:58



















  • Simular question like: stackoverflow.com/questions/1772941/… use the search function

    – episch
    Nov 22 '18 at 8:56











  • Please show minimum effort

    – mplungjan
    Nov 22 '18 at 8:57











  • Alternative search

    – mplungjan
    Nov 22 '18 at 8:58

















Simular question like: stackoverflow.com/questions/1772941/… use the search function

– episch
Nov 22 '18 at 8:56





Simular question like: stackoverflow.com/questions/1772941/… use the search function

– episch
Nov 22 '18 at 8:56













Please show minimum effort

– mplungjan
Nov 22 '18 at 8:57





Please show minimum effort

– mplungjan
Nov 22 '18 at 8:57













Alternative search

– mplungjan
Nov 22 '18 at 8:58





Alternative search

– mplungjan
Nov 22 '18 at 8:58












3 Answers
3






active

oldest

votes


















3














You could look for four characters with a positive lookahead for more characters and insert a space after each block.






function format(s) {
return s.toString().replace(/d{4}(?=.)/g, '$& ');
}

console.log(format(1234567890123456));








share|improve this answer































    3














    You can use RegEx for this






    let dummyTxt='1234567890123456';

    let joy=dummyTxt.match(/.{1,4}/g);
    console.log(joy.join(' '));








    share|improve this answer





















    • 1





      It do not add a space, it split it up

      – Grégory NEUT
      Nov 22 '18 at 8:57






    • 2





      Yes, @GrégoryNEUT I have edited my answer now.

      – Aravind Bhat K
      Nov 22 '18 at 9:02



















    2














    Without regex, you can use map as well to achieve this






    let x = '1234567890123456'

    let res = [...x].map((d, i) => (i) % 4 == 0 ? ' ' + d : d).join('').trim()

    console.log(res)








    share|improve this answer






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You could look for four characters with a positive lookahead for more characters and insert a space after each block.






      function format(s) {
      return s.toString().replace(/d{4}(?=.)/g, '$& ');
      }

      console.log(format(1234567890123456));








      share|improve this answer




























        3














        You could look for four characters with a positive lookahead for more characters and insert a space after each block.






        function format(s) {
        return s.toString().replace(/d{4}(?=.)/g, '$& ');
        }

        console.log(format(1234567890123456));








        share|improve this answer


























          3












          3








          3







          You could look for four characters with a positive lookahead for more characters and insert a space after each block.






          function format(s) {
          return s.toString().replace(/d{4}(?=.)/g, '$& ');
          }

          console.log(format(1234567890123456));








          share|improve this answer













          You could look for four characters with a positive lookahead for more characters and insert a space after each block.






          function format(s) {
          return s.toString().replace(/d{4}(?=.)/g, '$& ');
          }

          console.log(format(1234567890123456));








          function format(s) {
          return s.toString().replace(/d{4}(?=.)/g, '$& ');
          }

          console.log(format(1234567890123456));





          function format(s) {
          return s.toString().replace(/d{4}(?=.)/g, '$& ');
          }

          console.log(format(1234567890123456));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 8:56









          Nina ScholzNina Scholz

          187k1596172




          187k1596172

























              3














              You can use RegEx for this






              let dummyTxt='1234567890123456';

              let joy=dummyTxt.match(/.{1,4}/g);
              console.log(joy.join(' '));








              share|improve this answer





















              • 1





                It do not add a space, it split it up

                – Grégory NEUT
                Nov 22 '18 at 8:57






              • 2





                Yes, @GrégoryNEUT I have edited my answer now.

                – Aravind Bhat K
                Nov 22 '18 at 9:02
















              3














              You can use RegEx for this






              let dummyTxt='1234567890123456';

              let joy=dummyTxt.match(/.{1,4}/g);
              console.log(joy.join(' '));








              share|improve this answer





















              • 1





                It do not add a space, it split it up

                – Grégory NEUT
                Nov 22 '18 at 8:57






              • 2





                Yes, @GrégoryNEUT I have edited my answer now.

                – Aravind Bhat K
                Nov 22 '18 at 9:02














              3












              3








              3







              You can use RegEx for this






              let dummyTxt='1234567890123456';

              let joy=dummyTxt.match(/.{1,4}/g);
              console.log(joy.join(' '));








              share|improve this answer















              You can use RegEx for this






              let dummyTxt='1234567890123456';

              let joy=dummyTxt.match(/.{1,4}/g);
              console.log(joy.join(' '));








              let dummyTxt='1234567890123456';

              let joy=dummyTxt.match(/.{1,4}/g);
              console.log(joy.join(' '));





              let dummyTxt='1234567890123456';

              let joy=dummyTxt.match(/.{1,4}/g);
              console.log(joy.join(' '));






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 '18 at 9:01

























              answered Nov 22 '18 at 8:56









              Aravind Bhat KAravind Bhat K

              2741214




              2741214








              • 1





                It do not add a space, it split it up

                – Grégory NEUT
                Nov 22 '18 at 8:57






              • 2





                Yes, @GrégoryNEUT I have edited my answer now.

                – Aravind Bhat K
                Nov 22 '18 at 9:02














              • 1





                It do not add a space, it split it up

                – Grégory NEUT
                Nov 22 '18 at 8:57






              • 2





                Yes, @GrégoryNEUT I have edited my answer now.

                – Aravind Bhat K
                Nov 22 '18 at 9:02








              1




              1





              It do not add a space, it split it up

              – Grégory NEUT
              Nov 22 '18 at 8:57





              It do not add a space, it split it up

              – Grégory NEUT
              Nov 22 '18 at 8:57




              2




              2





              Yes, @GrégoryNEUT I have edited my answer now.

              – Aravind Bhat K
              Nov 22 '18 at 9:02





              Yes, @GrégoryNEUT I have edited my answer now.

              – Aravind Bhat K
              Nov 22 '18 at 9:02











              2














              Without regex, you can use map as well to achieve this






              let x = '1234567890123456'

              let res = [...x].map((d, i) => (i) % 4 == 0 ? ' ' + d : d).join('').trim()

              console.log(res)








              share|improve this answer




























                2














                Without regex, you can use map as well to achieve this






                let x = '1234567890123456'

                let res = [...x].map((d, i) => (i) % 4 == 0 ? ' ' + d : d).join('').trim()

                console.log(res)








                share|improve this answer


























                  2












                  2








                  2







                  Without regex, you can use map as well to achieve this






                  let x = '1234567890123456'

                  let res = [...x].map((d, i) => (i) % 4 == 0 ? ' ' + d : d).join('').trim()

                  console.log(res)








                  share|improve this answer













                  Without regex, you can use map as well to achieve this






                  let x = '1234567890123456'

                  let res = [...x].map((d, i) => (i) % 4 == 0 ? ' ' + d : d).join('').trim()

                  console.log(res)








                  let x = '1234567890123456'

                  let res = [...x].map((d, i) => (i) % 4 == 0 ? ' ' + d : d).join('').trim()

                  console.log(res)





                  let x = '1234567890123456'

                  let res = [...x].map((d, i) => (i) % 4 == 0 ? ' ' + d : d).join('').trim()

                  console.log(res)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 8:57









                  Nitish NarangNitish Narang

                  2,9601815




                  2,9601815















                      Popular posts from this blog

                      'app-layout' is not a known element: how to share Component with different Modules

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      WPF add header to Image with URL pettitions [duplicate]