I have more than 5 input text field to accept only comm/dot and only numbers.
[(ngModel)]="company.internetbill"
[(ngModel)]="company.electricitybill"
[(ngModel)]="company.waterbill"
etc..
I wrote a custom directive for the above validation. it works fine, My requirement is "when user input a -Comma- it must change decimal(dot).
commtodecimal(ref:any){
if(ref.includes(",")){
ref=ref.replace(",",".")
this.user.internetbill=ref
}
is it possible to change the ngModel value in the same custom directive . so that i can use same directive for both features.
My Custom Directive is--
private regex: RegExp = new RegExp(/^[+-]?\d*[\.\,]?\d{0,8}$/g);
private specialKeys: Array<string> = ['Backspace','ArrowLeft','ArrowRight','Delete'];
constructor(private el:ElementRef) { }
@HostListener('keydown',['$event'])onKeyDown(event:KeyboardEvent){
if (this.specialKeys.indexOf(event.key) !== -1) {
return;
}
const inputValue=this.el.nativeElement.value.concat(event.key)
if(inputValue && !String(inputValue).match(this.regex)){
event.preventDefault();
}
return
}
or please suggest any optimized way. so that can reuse the code