[SOLVED] How to count input value length in typescript(Angular)?

Issue

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

          <div class="col-md-3">
            <input type="text" class="form-control" 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?

Solution

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.

Answered By – Chaka15

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *