Angular Material Textarea Character Count
April 17, 2024
In Angular Material, text area is created using <textarea>
element with matInput
attribute.
To find the character count of textarea, access the text using template variable,
NgModel
or FormControl
and then find the length of text using JavaScript length
property of string.
To limit the number of characters, use
maxLength
validation.
Using Template Variable
Here I will use template variable to access textarea value.<textarea matInput #myText></textarea>
myText
is the template variable. Access textarea value as below.
{{myText.value.length}}
To limit the number of characters, specify
maxLength
as below.
<textarea matInput maxLength="30" #myText></textarea>
<p> {{30 - myText.value.length}} / 30 </p>
Using NgModel
Here I will useNgModel
to access textarea value.
TS code:
msgCharLength = 30; comment = "";
<textarea matInput [maxLength]="msgCharLength" [(ngModel)] = "comment"></textarea>
{{msgCharLength - comment.length}} / {{msgCharLength}}
Using FormControl
Here I will useFormControl
to access textarea value.
TS code:
desc = new FormControl("", [Validators.maxLength(this.msgCharLength)]);
<textarea matInput [formControl]="desc"></textarea>
{{desc.value?.length}}
<div *ngIf="desc.hasError('maxlength')" class="error"> Character exceeded. </div>
Example
Find a complete demo to count characters of textarea.user.component.html
<h3>1. With template variable</h3> <mat-form-field> <mat-label>Message: </mat-label> <textarea matInput [maxLength]="msgCharLength" placeholder="Write Here" #myText></textarea> </mat-form-field> <p>Character count: {{myText.value.length}} </p> <p>Remaining character: {{msgCharLength - myText.value.length}} / {{msgCharLength}} </p> <h3>2. With NgModel</h3> <mat-form-field> <mat-label>Comment: </mat-label> <textarea matInput [maxLength]="msgCharLength" [(ngModel)]="comment" placeholder="Write Here"></textarea> </mat-form-field> <p>Character count: {{comment.length}} </p> <p>Remaining character: {{msgCharLength - comment.length}} / {{msgCharLength}} </p> <h3>3. With FormControl</h3> <mat-form-field> <mat-label>Description: </mat-label> <textarea matInput [formControl]="desc" placeholder="Write Here"></textarea> </mat-form-field> <p>Character count: {{desc.value?.length}} </p> <div *ngIf="desc.hasError('maxlength')" class="error"> Character exceeded. Max length is {{msgCharLength}}. </div>
import { Component } from '@angular/core'; import { MatInputModule } from '@angular/material/input'; import { MatFormFieldModule } from '@angular/material/form-field'; import { FormControl, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-user', standalone: true, imports: [MatFormFieldModule, MatInputModule, FormsModule, ReactiveFormsModule, CommonModule], templateUrl: './user.component.html' }) export class UserComponent { msgCharLength = 30; comment = ""; desc = new FormControl("", [Validators.maxLength(this.msgCharLength)]); }