0
I have a combo "Person Type" with options (1 - Physical | 2 - Legal) and I need to limit the default character size to be filled in the text field "CPF/CNPJ".
In fact, I need to validate the text field for when the person type "1 - Physics" is selected in the combo, the maxlength is the default for the CPF, and so for the same to the CNPJ.
The Form
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-2">
<div class="form-group">
<label for="tipoPessoa">Tipo Pessoa:*</label>
<select
id="tipoPessoa"
name="tipoPessoa"
data-ng-required="true"
data-ng-model="vm.tipoPessoa"
class="form-control input-sm"
data-ng-options="opt as opt.tipoPessoa for opt in options">
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-3">
<div class="form-group">
<label for="cpfcnpj">CPF/CNPJ:*</label>
<input type="text"
id="cpfcnpj"
name="cpfcnpj"
data-ng-required="true"
data-ng-model="vm.simulacao.cpfcnpj"
required="required"
class="form-control input-sm" />
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-3">
<button type="button" class="btn btn-sm btn-primary"
data-ng-click="vm.limparSelecionados(); vm.pesquisar();"
data-ng-disabled="Form.$invalid" style="margin-top: 22px;"
data-aria-label="Right Align">
<i class="glyphicon glyphicon-search"></i> <span>Pesquisar</span>
</button>
</div>
https://answall.com/a/153692/21112
– Pedro Augusto
@Pedroaugusto just one observation, the answer accepted in this other question is not the best option. Have better answers down with higher score
– Sorack
Possible duplicate of Validation of CNPJ with Angularjs
– Sorack
Precisely, why I put a link referencing a better answer
– Pedro Augusto
Guys, the CPF and CNPJ validation is already working. My question is to carry out the limitation of the text field for the user’s completion from the selection of the Type Person in the combo.
– aleestevao
Do you just want to limit the amount of characters in the field? Isn’t it much easier to apply a mask? The best response of the first link provided by Pedro Ugusto shows exactly this, besides validating the Cpf format on top of which forces the person to fill in the exact amount of characters, no more and no less.
– Máttheus Spoo
That @Máttheusspoo, just limit the amount of characters in the text field. After selecting in combo whether it is a natural or legal person to trigger the limiter (maxlength) according to the standard size.
– aleestevao
you can touch the maxlength simply with
document.querySelector('#id').setAttribute('maxlength', 11) // exemplo pra cpf
or in the case of jquery `– Máttheus Spoo