I want to make typescript validation to this: 01-23-456789
I have one for account number :
this.contractForm.controls.account.get('accNum').setValue(val.match(/[0-9]{1,8}/g)?.join('-'))
This is easy because the account numbers look like this :
12345678-12345678-12345678
And I'm not a regex expert unfortunately, and I don't know how to make for this, that I mentioned in my first sentence.
public businessNumberValidator() {
return this.contractForm.controls.szerzodo
.get('businessnum')
.valueChanges.pipe(
throttleTime(1),
tap((val: string) => {
const char = val.replace(/\D/g, '').split('');
if (char.length > 2) {
char.splice(2, 0, '-');
}
if (char.length > 5) {
char.splice(5, 0, '-');
}
this.contractForm.controls.szerzodo.get('businessnum').setValue(char.join(''));
}),
untilDestroyed(this)
)
.subscribe();
}
and I write this kind of solution, but I want a better one
^\d+(?:-\d+)+$regex101.com/r/greKgy/1