Generic email validator

后端 未结 10 909
灰色年华
灰色年华 2020-11-30 02:01

I want to create a form where the user will enter his email. I\'d like to validate email format client-side.

Is there any generic email validator in Angular 2?

10条回答
  •  粉色の甜心
    2020-11-30 02:33

    I guess just now there is no email validator, but it's pretty easy to add a custom one. See this demo I used the same regex as angular1 uses.

    function emailValidator(control) {
      var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
    
      if (!EMAIL_REGEXP.test(control.value)) {
        return {invalidEmail: true};
      }
    }

提交回复
热议问题