There is some restrictions in name field so I am trying to validate name field using directive as below. Inside directive I am using regular expression to check valid name and then replacing valid name into textbox using valueAccessor.writeValue(newVal)
Here issue is when I am trying to type middle of some word in textbox cursor jump at the end.
@Directive({
selector: '[validateName]',
host: {
'(ngModelChange)': 'onInputChange($event, false)',
'(keydown.backspace)': 'onInputChange($event.target.value, true)',
'(focusout)': 'removeClass()'
}
})
export class NameValidator {
constructor(public model: NgControl,public renderer: Renderer, public el: ElementRef) { }
onInputChange(event, backspace) {
if (!backspace) {
// Remove invalid characters (keep only valid characters)
var newVal = event.replace(/^[0-9\s]/g, '').replace(/[^A-Za-z0-9_$]/g,'');
// Add class for invalid name.
if (/^[0-9\s]/g.test(event) || /[^A-Za-z0-9_$]/g.test(event)) {
this.renderer.setElementClass(this.el.nativeElement, 'invalid-name', true);
}
else {
this.renderer.setElementClass(this.el.nativeElement, 'invalid-name', false);
}
// set the new value
this.model.valueAccessor.writeValue(newVal);
}
}
removeClass() {
this.renderer.setElementClass(this.el.nativeElement, 'invalid-name', false);
}
}
That comes from the fact DefaultValueAccessor
writes blindly the element value, if there is a focus or not, selection or not. I had to do deal with such a behavior myself as I was using a form that was auto-saving and I had to override DefaultValueAccessor
and create one that was only writing value if it was different (I don't think that would work for you, see below) :
export const DEFAULT_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DefaultValueAccessor),
multi: true
};
@Directive({
selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]",
host: {"(input)": "onChange($event.target.value)", "(blur)": "onTouched()"},
providers: [DEFAULT_VALUE_ACCESSOR]
})
export class DefaultValueAccessor implements ControlValueAccessor {
onChange = (_: any) => {
};
onTouched = () => {
};
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {
}
writeValue(value: any): void {
const normalizedValue = value == null ? "" : value;
// line bellow is the only line I added to the original one
if ((this._elementRef.nativeElement as HTMLInputElement).value !== normalizedValue)
this._renderer.setElementProperty(this._elementRef.nativeElement, "value", normalizedValue);
}
registerOnChange(fn: (_: any) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._renderer.setElementProperty(this._elementRef.nativeElement, "disabled", isDisabled);
}
}
For your case, you might need to deal with input selection :
let start=this.el.nativeElement.selectionStart;
let end = this.el.nativeElement.selectionEnd;
this.model.valueAccessor.writeValue(newVal);
this.el.nativeElement.setSelectionRange(start,end);
Note that it might not be accurate as you are modifying the input...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With