How to Add space between every 4 characters in JavaScript? [duplicate]
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
javascript html split
marked as duplicate by mplungjan
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.
add a comment |
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
javascript html split
marked as duplicate by mplungjan
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
add a comment |
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
javascript html split
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
javascript html split
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
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
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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));
add a comment |
You can use RegEx for this
let dummyTxt='1234567890123456';
let joy=dummyTxt.match(/.{1,4}/g);
console.log(joy.join(' '));
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
add a comment |
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)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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));
add a comment |
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));
add a comment |
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));
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));
answered Nov 22 '18 at 8:56
Nina ScholzNina Scholz
187k1596172
187k1596172
add a comment |
add a comment |
You can use RegEx for this
let dummyTxt='1234567890123456';
let joy=dummyTxt.match(/.{1,4}/g);
console.log(joy.join(' '));
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
add a comment |
You can use RegEx for this
let dummyTxt='1234567890123456';
let joy=dummyTxt.match(/.{1,4}/g);
console.log(joy.join(' '));
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
add a comment |
You can use RegEx for this
let dummyTxt='1234567890123456';
let joy=dummyTxt.match(/.{1,4}/g);
console.log(joy.join(' '));
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(' '));
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 22 '18 at 8:57
Nitish NarangNitish Narang
2,9601815
2,9601815
add a comment |
add a comment |
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