I have a ViewModel containing a string CreditCard. What I am looking for is an attribute or masking it in a way that in every 4 digits one space is added . Something like this: 4764 4000 9425 3041 Any suggestions?
Asked
Active
Viewed 1.5k times
0
-
Welcome to Stack Overflow! You should probably wait to ask your question until you have made an attempt at this and have code to share along with a specific question about an error or problem with your code. – Sam W Dec 07 '18 at 15:49
-
Try searching for some kind of jQuery / CSS input masking techniques and implement a blanking/ starring out code. Maybe there is a plugin that does that but needs more searching. You could just write some javascript your self to do this, when a value typed in, it moves that value to a hidden input and * out the visible one. And the same when deleting. – Piotr Kula Dec 07 '18 at 16:00
-
have you tried to use this https://igorescobar.github.io/jQuery-Mask-Plugin/ – Alireza Yadegari Dec 07 '18 at 16:02
-
or https://github.com/RobinHerbots/Inputmask or https://codepen.io/estelle/pen/LVQLRq ? – Piotr Kula Dec 07 '18 at 16:03
-
are you asking for c# or javascript? I dont think your task is related with asp.net-core – Derviş Kayımbaşıoğlu Dec 07 '18 at 16:53
3 Answers
1
Robin Herbot's jQuery input mask (https://github.com/RobinHerbots/Inputmask) seems to do what you're looking for. You can specify the mask formatting in javascript or put it as an attribute.
<input id="cc" type="text" data-inputmask="'mask': '9999 9999 9999 9999'" />
Here is a codepen example (disclaimer not mine, credit to: Chris Coyier):

Tom Dee
- 2,516
- 4
- 17
- 25
0
Here is a way to do it with javascript:
var num = 1215464565;
var newNum = num.toString().match(/.{4}/g).join(' ');
console.log(newNum);

Ben
- 1,820
- 2
- 14
- 25
0
try this:
const num = 4764400094253041;
const regex = /\d{4}/g;
const res = num.toString().replace(regex, (maths) => maths === 12 ? maths : maths + ' ')
const responseEl = document.getElementById("response");
responseEl.innerText = res;
<div id="response"></div>

Caio cezar
- 11
- 1
- 2