Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put character limit on a mat Input?

I didn't find any mention of character limit in the docs. Seems like a very common requirement.

How Can I add a 300 characters limit to this Textarea ?

https://stackblitz.com/edit/angular-izbcey?file=app/input-overview-example.ts

like image 629
dota2pro Avatar asked Sep 17 '19 14:09

dota2pro


People also ask

How do I reduce the size of a mat form field?

The key to adjusting the field size is actually just adjusting the font-size in the surrounding container. Once you do that, everything else will scale with it.

What is matInput?

Matinput is an Angular directive that primarily allows input and text area elements to work with a form field. With this, you can display placeholders perfectly, add custom error messages, a clear button, specify the maximum length of the text or add prefixes and suffixes for a seamless user experience.


1 Answers

You can use like this for text

<mat-form-field hintLabel="Max 300 characters">
<input matInput #input maxlength="300" placeholder="Enter some input">
<mat-hint align="end">{{input.value?.length || 0}}/300</mat-hint>
</mat-form-field>

for textarea

<mat-form-field hintLabel="Max 10 characters">
  <textarea #txtarea matInput  [maxLength]="10" [placeholder]="label"></textarea>
 </mat-form-field>
like image 121
Zulqarnain Jalil Avatar answered Sep 22 '22 15:09

Zulqarnain Jalil