I have implemented the directive to restrict the input field using angular2. It is working fine in desktop browser, but not working in android mobile.
import { LimitToDirective } from '../../../directives/limitedvalidation';
@Component({
templateUrl: 'build/pages/profile/change-basic-details/change-basic-details.html',
directives: [LimitToDirective]
})
export class UpdateBasicDetailsPage {
constructor(){}
}
import {Directive, Input} from '@angular/core'
@Directive({
selector: '[limit-to]',
host: {
'(keypress)': '_onKeypress($event)',
}
})
export class LimitToDirective {
@Input('limit-to') limitTo;
_onKeypress(e) {
const limit = +this.limitTo;
if (e.target.value.length === limit) e.preventDefault();
}
}
<ion-input limit-to="12" type="tel" [formControl]="aadhaarno">
Use maxlength as belows :
<ion-input [maxlength]="12" type="tel" [formControl]="aadhaarno">
you should use max-length html5 attribute and then use form validation for Angular2, no custom directive needed.
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