I have a situation where I need to format
the user input and then validate
it.
I am using a reactive form
and created my custom validation
as seen below (relevant parts):
HTML:
<input type="text" formControlName="invoiceNumber" (blur)="formatInvoiceNumber()" class="form-control">
<div *ngIf="this.form.controls['invoiceNumber'].invalid && this.form.controls['invoiceNumber'].touched">Invalid Text</div>
Controller:
this.form = this.formBuilder.group({
'invoiceNumber': ['', validateInvoiceNumber()],
});
formatRoNumber() {
var invoiceNumber = this.form.controls['invoiceNumber'].value;
//format invoice number
}
Validator:
export function validateInvoiceNumber(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
let invoiceNumber = control.value,
isValid = true;
//validate invoice number
return isValid ? null : { validInvoiceNumber: { valid: false } }
};
}
I am running into timing issues. The formatting of input happens after validation.
How can I tell Angular
to apply the formatting
and then validate
?