【问题标题】:Does HTML5 email validation and angular2 work together?HTML5 电子邮件验证和 angular2 是否一起工作?
【发布时间】:2016-08-02 11:24:28
【问题描述】:

我目前正在使用 angular2 进行验证。我对 HTML5 电子邮件和网站验证器以及 valid ngModel 的属性有一些问题。

例如:

<form #form="ngForm">
    <input type="email" #email="ngModel" [(ngModel)]="contact.email" name="email" >
    <button [disabled]="!form.form.valid" type="submit">Btn</button>

我输入的每个单词都很好。 #email.valid 仍然为真,就好像不存在 HTML5 验证器一样:

{{#email.valid}} %%% true

所以表单的按钮一直处于启用状态。但是当我点击按钮时,会出现 HTML 警告,指出电子邮件字段无效,因此验证正在运行,但 #email.valid 它仍然是正确的。

是否可以将 angular2 的 ngModel 指令与 HTML5 验证器一起使用?

【问题讨论】:

    标签: html angular html5-validation


    【解决方案1】:

    是的,你可以同时使用它们,你可以像这样使用它们

    <input  id="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" class="form-control" type="email" ng-model="loginctrl.user.email" name="email" placeholder="Enter Email Address" required/>
    <span ng-show="myForm.email.$touched && myForm.email.$error.required"><b class="color">This is a required field</b></span> <span ng-show=" myForm.$dirty && myForm.email.$invalid"><b class="color">This field is invalid</b></span>
    

    【讨论】:

    • 小心,这不是有效的电子邮件模式。不接受 .info 等顶级域!
    • 问题是关于angular2,答案是angular1
    猜你喜欢
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2021-12-28
    • 2011-06-23
    • 1970-01-01
    • 2015-11-09
    相关资源
    最近更新 更多