Am creating a custom reactive form from dynamic JSON value, trying to achieve hide the character what the user entered in text box to asterisk symbol,
Example: lets say user is typing "hello world".
Expected output: **********, this should come when user types each and every character
there will be also pattern validation like alphabet only and number only,
I have achieved the above Expected Output, but the validation is not working as expected
Code What i have Tried
SetValidations(val: any) {
this.formBuilder.control(val.key);
this.newGeographyForm.addControl(
val.key.toString(),
new FormControl('')
);
let validators = this.getValidatorFnArray(val.templateOptions)
debugger
this.newGeographyForm.get(val.key).setValidators(validators)
val.templateOptions.input_mask > 0 ?this.MaskValue(val.key,val.templateOptions.input_mask) : ''
}
MaskValue(formControlVal:any,maskType:any){
const regex = /\S/gi;
const subst = `*`;
this.newGeographyForm.get(formControlVal).valueChanges.pipe(distinctUntilChanged()).subscribe(fVal=>{
const result = fVal.replace(regex, subst);
this.newGeographyForm.get(formControlVal).patchValue( result, {emitEvent: false});
this.newGeographyForm.get(formControlVal).updateValueAndValidity();
})
}
getValidatorFnArray(obj): ValidatorFn[] {
const validators = [];
if (obj.is_mandatory) {
debugger
validators.push(Validators.required);
}
if (obj.min != null) {
debugger
validators.push(Validators.minLength(obj.min));
}
if (obj.max != null) {
validators.push(Validators.maxLength(obj.max));
}
if (obj.regex != null) {
validators.push(Validators.pattern(obj.regex));
}
return validators
}