1
I have three fields and I have to validate sending the message ng-mensage
of the first field whenever a value is exchanged and preventing the form from being submitted.
The sum of the field número 2
and número 3
cannot be greater than the field value número 1
.
How do I validate?
<form name="form">
<md-input-container >
<label>Numero1</label>
<input required=""
name="numero1"
ng-model="obj.numero1"
ng-pattern="/^[0-9]{0,10}$/">
<div ng-messages="form.numero1.$error">
<div ng-message="required">Este campo é obrigatório.</div>
<div ng-message="pattern">Apenas números</div>
</div>
</md-input-container>
<md-input-container>
<label>Número 2</label>
<input required=""
name="numero2"
ng-model="obj.numero2"
ng-pattern="/^[0-9]{0,10}$/">
<div ng-messages="form.numero2.$error" >
<div ng-message="required">Este campo é obrigatório.</div>
<div ng-message="pattern">Apenas números</div>
</div>
</md-input-container>
<md-input-container >
<label>Número 3</label>
<input required=""
name="numero3"
ng-model="obj.numero3"
ng-pattern="/^[0-9]{0,10}$/">
<div ng-messages="form.numero3.$error" >
<div ng-message="required">Este campo é obrigatório.</div>
<div ng-message="pattern">Apenas números</div>
</div>
</md-input-container>
</form>
Fabio Luis Alexandre thanks for the reply. I have to validate so q move to another field and display in ng-message
– marcao1569
Ideal then is to create a directive to do this for you, where is passed the name of the field to be checked and an object with the errors to be displayed. you can control by ng-if="ngModelController. $$parentForm. $submitted || ngModelController. $Touched" https://scotch.io/tutorials/angularjs-form-validation-with-ngmessages This link is really nice for you to study and apply.
– Fabio Luis Alexandre