0

i use textarea tag and i want when user type in that, she can not type more than forexample 300 character. i write code and i have real time character number that user type but i want when it comes to 300 character she can not type anymore.

here is my html code:

<textarea type="text" style="margin-bottom: 25px" id="textAreaScroll" formControlName="text" placeholder="متن ..." (keyup)="postLengthCheck()"> </textarea> <span *ngIf="createPostForm.controls['text'].valid" class="text-counter">{{validTextLength}}</span> 

and here is my postLengthCheck() method im my component:

postLengthCheck() { if (this.createPostForm.controls['text'].value !== null) { this.postLength = this.createPostForm.controls['text'].value.length; this.validTextLength = 2200 - this.postLength; } } 

how can i fix this ?

1
  • Just use maxlength='300' property in your <textarea> that might work Commented Nov 7, 2018 at 9:56

3 Answers 3

1

you can add a maxLength validator when creating your formControl like this :

new FormControl('text', [Validators.maxLength(100)]); 

Regards,

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer but my main problem is i want user can not type in textbox anymore in this situation and i don't know how to do this
0

You can just use maxlength='300' prop in you <textarea> like this

 <textarea type="text" style="margin-bottom: 25px" id="textAreaScroll" formControlName="text" placeholder="متن ..." maxLength="300"> </textarea> 

if you are reading the value from Component class just use attribute binding in [maxlength]='your_prop_name' - this might work

Thanks happy coding !!

Comments

0

Why not use [maxLength]="'300'"

<textarea type="text" style="margin-bottom: 25px" id="textAreaScroll" formControlName="text" placeholder="متن ..." (keyup)="postLengthCheck()" [maxLength]="'300'"`> </textarea> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.