简体   繁体   English

Mat-chip-list的电子邮件验证

[英]Email validation for Mat-chip-list

I am trying to apply email validation to mat-chip. 我正在尝试将电子邮件验证应用于mat-chip。 Below is my attempt to achieve the same. 以下是我尝试实现同样的目标。

https://stackblitz.com/edit/mat-chip-demo-yvmxsk https://stackblitz.com/edit/mat-chip-demo-yvmxsk

My requirement is to display an mat-error when user enters an invalid email ID and highlight only invalid email ID. 我的要求是当用户输入无效的电子邮件ID并仅突出显示无效的电子邮件ID时显示mat-error。 Can someone help me on the same? 有人可以帮助我吗?

You can achieve this by setting a property on the added email value such as invalid. 您可以通过在添加的电子邮件值(例如无效)上设置属性来实现此目的。

add(event): void {
    console.log(event.value)
    if (event.value) {
      if (this.validateEmail(event.value)) {
        this.emailList.push({value:event.value, invalid:false});
      }else{
        this.emailList.push({value:event.value, invalid:true}); 
      }
    }
    if (event.input) {
      event.input.value = '';
    }
}

then, you can highlight the chip by dynamically setting the color: 然后,您可以通过动态设置颜色来突出显示芯片:

<mat-chip [color]="(item.invalid)?'warn':''" selected />

Live Demo 现场演示

For more info about chips: https://material.angular.io/components/chips/overview 有关芯片的更多信息: https//material.angular.io/components/chips/overview

FYI, I used a simple email validator using regex: 仅供参考,我使用了一个使用正则表达式的简单电子邮件验证器:

private validateEmail(email) {
  var re = /^(([^<>()\[\]\\.,;:\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,}))$/;
    return re.test(String(email).toLowerCase());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM