Skip to content

Instantly share code, notes, and snippets.

@parollon
Last active February 1, 2024 08:58
Show Gist options
  • Select an option

  • Save parollon/fb286839c08fa4916240dba2bb079f29 to your computer and use it in GitHub Desktop.

Select an option

Save parollon/fb286839c08fa4916240dba2bb079f29 to your computer and use it in GitHub Desktop.
=======================
Code:
==============================================================================================================
@ViewChild('noNumericCharsInput1', { static: true }) noNumericCharsInput1!: IonInput;
@ViewChild('noNumericCharsInput2', { static: true }) noNumericCharsInput2!: IonInput;
...
@ViewChild('noNumericCharsInputN', { static: true }) noNumericCharsInputN!: IonInput;
viewChildsInputBehaviours: Subscription[] = [];
ngAfterViewInit() {
this.setNonNumericCharsRestriction(this.noNumericCharsInput1);
this.setNonNumericCharsRestriction(this.noNumericCharsInput2);
...
this.setNonNumericCharsRestriction(this.noNumericCharsInputN);
}
setNonNumericCharsRestriction(input: IonInput) {
let sub: Subscription = input.ionInput.subscribe((ev: any) => {
const value = ev.target.value;
const filteredValue = value.replace(/[^a-zA-Z]+/g, '');
input.value = filteredValue;
});
this.viewChildsInputBehaviours.push(sub);
}
ionViewWillLeave() {
for (let i = 0; i < this.viewChildsInputBehaviours.length; i++) {
const sub = this.viewChildsInputBehaviours[i];
sub.unsubscribe();
this.viewChildsInputBehaviours = []; }
}
=======================
Template:
==============================================================================================================
<ion-input type="text" #noNumericCharsInput1 ></ion-input>
<ion-input type="text" #noNumericCharsInput2 ></ion-input>
...
<ion-input type="text" #noNumericCharsInputN ></ion-input>
<ion-input type="text" ></ion-input
<ion-input type="text" ></ion-input
<ion-input type="text" #specificFormat ></ion-input>
<ion-input type="number" ></ion-input
<ion-input type="tel" ></ion-input>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment