Validate an input field, not null in HTML

0

I am working with the angular framework and in one of the components I have an html to do a login which I want to validate. I have tried to use the 'required' specifier and it does not affect it at all. Could you help me?

<input  type="text"
[(ngModel)]="usuario" 
placeholder="Usuario" 
name="usuario"
required />
    
asked by Boortx 19.04.2018 в 13:46
source

2 answers

1

A way to add a validation to the form with Angular is as follows:

<input  type="text" name="usuario" [(ngModel)]="usuario" #usuario="ngModel" 
placeholder="Usuario"  required />

<div *ngIf="usuario.invalid" class="alert alert-danger">
     <div *ngIf="usuario.errors.required">
        Ingrese su usuario
    </div>
</div>

#name="ngModel" Instance the input text, in order to use the validation statuses as required.

PDTA: Another way to validate is using reactive forms, I pass the official documentation of angular where it explains: link

    
answered by 20.04.2018 / 00:15
source
0

You can use it in the following way:

<input  type="text" [(ngModel)]="usuario" placeholder="Usuario" name="usuario"
required = true />
    
answered by 19.04.2018 в 17:46