Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 required attribute not working in Angular 2 project, installed using CLI

I am working on Angular 2 login page, when I try to validate input field with HTML 5 validate attribute required, it is not working, where as in the quick start project it works fine. Could any one explain why it is not working and how to resolve this issue ?

Plunker link of my code In Plunker, it validates but in my project its not working.

app.component.html code below,

<form class="login-form">
  <input type="text" name="usrname" placeholder="Username"  required/>

  <input type="password" name="pwd" placeholder="Password"  required>
  <input type="submit">

</form>
like image 912
Manjula D Avatar asked Mar 27 '17 10:03

Manjula D


People also ask

How to apply required validation in Angular?

To add validation to a template-driven form, you add the same validation attributes as you would with native HTML form validation. Angular uses directives to match these attributes with validator functions in the framework.

What is required in angular?

AngularJS ng-required DirectiveThe ng-required directive sets the required attribute of a form field (input or textarea). The form field will be required if the expression inside the ng-required attribute returns true. The ng-required directive is necessary to be able to shift the value between true and false .


1 Answers

As of version 4.0.0, Angular automatically adds "novalidate" to the form. Change this new default behavior by using:

<form ngNativeValidate>...</form>

Github issue here: https://github.com/angular/angular/issues/15531#issuecomment-289555359

like image 172
adova Avatar answered Oct 17 '22 10:10

adova