【问题标题】:Angular2 email validation pattern not workingAngular2电子邮件验证模式不起作用
【发布时间】:2017-08-21 15:02:53
【问题描述】:

表格:

    <div class="group">
      <label for="email" class="label">Email Address</label>
      <input id="email" type="text" class="input" name="email" pattern="/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" [(ngModel)]="user.email"
             #email="ngModel" required>
      <div *ngIf="email.errors && (email.dirty || email.touched)" class="alert alert-danger">
        <div [hidden]="!email.errors.required">
          email invalid
        </div>
      </div>
    </div>

我尝试使用正则表达式模式,但它没有工作,它没有显示任何错误,并且创建的用户没有有效的电子邮件地址。我从 stackoverflow 的其他 angular2 电子邮件验证答案中使用了这种模式。

【问题讨论】:

标签: angular


【解决方案1】:

试试这个...

if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this.email)){
   console.log('valid email');
}else{ 
   console.log('invalid email');
}

【讨论】:

  • 它对我有用。尝试修剪你的电子邮件必须有空格或任何东西。
【解决方案2】:
  <input id="email" type="text" class="input" name="email" pattern="/^(([^<>()\[\]\\.,;:\s@']+(\.[^<>()\[\]\\.,;:\s@']+)*)|('.+'))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/" [(ngModel)]="user.email"

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 1970-01-01
    • 2017-05-27
    • 2012-08-19
    • 2019-01-01
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多