i want set limit on number of integers in input field maxlength when type text. below code in template.
<ion-input type="number" placeholder="username (required)" [ngmodel]="test" (ngmodelchange)="getlength($event)" name="test"> </ion-input>
in .ts,
getlength(data){ if(data.length > 8){ console.log(1); let data1 = data.substring(0,8); this.test = data1; return false; } }
any idea how prevent event keypress when length greater 8.
thanks, ashley
use formbuilder
perform more complex validation, it'll save lot of time , effort.
import {formbuilder, formgroup} '@angular/forms'; constructor(private fb: formbuilder) {} public myform: formgroup = this.fb.group({ username: ['', [validators.required, validators.maxlength(8)]] });
and use like:
<!-- swap div whatever parent container --> <div> <ion-input type="number" formcontrolname="username"> </div>
then check validity of form with:
this.myform.valid
which evaluate either true
or false
depending on whether it's valid or not.
edit: add answer above, can use pipe:
import {pipe, pipetransform} '@angular/core'; @pipe({ name: 'maxlength' }) export class maxlength implements pipetransform { transform(value: string): string { if (value) { return value.length > 8 ? value.splice(0, 8) : value; } } }
i haven't tested might have tweak in order work properly. should give rough idea.
Comments
Post a Comment