Task: to make dynamic formatting user input for credit cards in Angular 2.
The problem is that when I was deleting a space between the figures, it is removed and does not appear in the input back, although values in deriving the console correct
My component
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
values: any = "";
input_text: string = "10";
tmp: string[] = [];
val: string;
creditCard(event: any) {
if ((/^3[47]\d{0,13}/).test(event.target.value)) {
this.values = "";
this.input_text = this.creditCardType.amex.text_length;
this.val = event.target.value;
this.val = this.val.replace(/[\W\s\._\-]+/g, '');
let chunk = [];
let split = 4;
for (var i = 0, len = this.val.length; i < len; i += split) {
split = ( i >= 4 && i <= 10 ) ? 6 : 4;
chunk.push( this.val.substr( i, split ) );
}
this.values = chunk.join(" ");
console.log(this.values); //after deleting in console: 3456 598756 58745
//on input: 3456 59875658745
}
}
}
app.component.html
<md-input-container>
<input mdInput placeholder="creditCard" (keyup)="creditCard($event)"[value]=values [attr.maxlength]=input_text numbers>
</md-input-container>
<p>{{values}}</p>
I need that space can not be removed or after removing it displays back on the input.