I'm trying to add a space after every 4 numbers in an input field.
here's my code :
credit: function(e) {
const val = e.target.value;
const valArray = val.split(' ').join('').split('');
if(isNaN(valArray.join('')))
return;
if(valArray.length === 17)
return
if(valArray.length % 4 === 0) {
this.setState({ number: e.target.value + " " });
}else{
this.setState({ number: e.target.value})
}
},
and here's the rules : user can write only numbers
and length should 16 and add space after each 4 numbers.
the problem is :
1: at the end of the numbers there is a extra space that added after the last number
2: i can't use backspace
to delete element (after pushing back space it will add space in front of the numbers)