0
I’m working on an Input that needs to have a message :
Type at least 10 characters
that message should change with each character that is typed
So, when you type:
Alex
the message should be:
Type at least 6 more characters
my problem is how to get the string size:
<input
type="text"
formControlName="nome"
class="form-control"
[ngClass]="{'is-invalid': submitted && nome.invalid && (nome.dirty || nome.touched), 'is-valid': (nome.valid && nome.touched ) }">
.
<div class="text-muted" *ngIf="nome?.value.length < 10 ">
digite pelo menos mais {{10 - nome?.value.length }} caracteres </div>
But when starting the "name" this Undefined, and it breaks the application,
any suggestions?
And how did you add the word "more"? When there is something typed, according to the question, the phrase needs to be "at least plus X characters". And if 12 characters are typed, "at least plus -2 characters"?
– Woss
I set the question, , now the word 'more' exists from the beginning. .
– Alexandre Queiroz
Um, instead of adapting the solution you adapt the problem? haha xD
– Woss
Use the command
(input)
on the taginput
to update the value. Ex:html5
<input
type="text"
formControlName="nome" 
class="form-control"
(input)="contaCaracteres()"
[ngClass]="{'is-invalid': submitted && nome.invalid && (nome.dirty || nome.touched), 'is-valid': (nome.valid && nome.touched ) }">

Ai in the component, inside the function contaCaracteres(), you can domeuForm.get('nome').value.length
and store the return value in another variable. It’s a way for you to always count the characters of a string in the input.– Edward Ramos
Or, as ta using Formgroup, you can try using the command
meuForm.get('nome').value.length
on the div that will show the message– Edward Ramos