最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 required attribute not working in Angular 2 project, installed using CLI - Stack Overflow

programmeradmin3浏览0评论

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.

appponent.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>

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>
Share Improve this question edited Mar 27, 2017 at 12:23 Manjula D asked Mar 27, 2017 at 10:59 Manjula DManjula D 1452 silver badges8 bronze badges 13
  • what you expected to see?? Please explain why it is not working? We can't by looking at this generic statement.. – Pankaj Parkar Commented Mar 27, 2017 at 11:03
  • can you recreate you problem on plunker ? – Pardeep Jain Commented Mar 27, 2017 at 11:03
  • have you added <!doctype html> to your index page? – Sriram Commented Mar 27, 2017 at 11:10
  • 1 do you have novalidate on the <form> tag? can you show the whole template and component? – Jason White Commented Mar 27, 2017 at 11:21
  • 1 when using angulars built in validation you need to disable the browswers default validation by adding novalidate to the form tag <form novalidate>...</form> – Jason White Commented Mar 27, 2017 at 11:24
 |  Show 8 more comments

2 Answers 2

Reset to default 27

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

     <form class="login-form" novalidate> 
    <input type="text" name="usrname" placeholder="Username" required="required"> 
<input type="password" name="pwd" placeholder="Password" required="required"> 
<button type="submit">Submit </button> 
    </form> 

remove novalidate from your form tag to enable html5 validation

<form class="login-form" >

If you use novalidate in your form tag, the validation wont occur as that's that the novalidate keyword is for when you don't want the default html5 validation and you want to implement your own validation means you have to use the novalidate otherwise you don't need it

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论