Home > Software design >  How to count input value length in typescript(Angular)?
How to count input value length in typescript(Angular)?

Time:03-24

I want to count form control value in reactive form..

          <div >
            <input type="text"  formControlName="PreMobile" (keypress)="keyPress($event)" (focusout)="validation()"/>
          </div>

This is my Form Control Name.

Form Name is memberApplicationForm

Form Initialization

private initializeForm() {
    this.memberApplicationForm = new FormGroup({
      PreMobile: new FormControl(''),
    });

Now I want to count PreMobile formcontrol input value length.

  validation(){
    // What should be here
  }

How can I do that?

CodePudding user response:

This should do the work:

validation() {
   console.log(this.memberApplicationForm.controls.PreMobile.value.length);
}

On the form object we can access its controls with their respective values. Try logging this like above and use it as you intended.

CodePudding user response:

Use like this,

validation() {
   console.log(this.memberApplicationForm.get('PreMobile').value.length);
}
  • Related