Are you tired of receiving invalid emails in your Angular 8 application? Look
no further than email validation regex in Angular 8. In this article, we will
explore how to use email validation regex in your Angular 8 forms to ensure
that your users enter valid email addresses.

What is Email Validation Regex?

email-validation
Regex, or regular expressions, are a sequence of characters that define a
search pattern. Email validation regex is a specific search pattern used to
validate email addresses. It checks if an email address is valid or not based
on certain criteria such as the format of the email address, the presence of
the "@" symbol, and the existence of the domain name.

Using Email Validation Regex in Angular 8

Angular 8 provides built-in email validation using the EmailValidator class.
However, you can also use email validation regex to customize the validation
criteria. To do this, you need to create a custom validator using the
Validators class and the pattern method.

import { Validators } from '@angular/forms';

export const emailValidator = [Validators.pattern(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)];

In this example, we create a custom validator called emailValidator that uses
email validation regex to validate email addresses. We pass the regex pattern
as an argument to the pattern method, which returns a validator function that
can be used in an Angular 8 form.

Implementing Email Validation Regex in Angular 8 Forms

email-validation
Now that we have created a custom validator using email validation regex, we
can use it in our Angular 8 forms. To do this, we first need to import the
custom validator and add it to the validators array of our form control.

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { emailValidator } from './validators/email.validator';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
emailForm: FormGroup;

constructor(private fb: FormBuilder) {
this.emailForm = this.fb.group({
email: ['', [Validators.required, emailValidator]]
});
}
}

In this example, we import the FormBuilder, FormGroup, Validators, and
emailValidator classes. We then create a FormGroup called emailForm that
contains a single form control called email. We pass the emailValidator class
as an argument to the validators array of the email form control to validate
the email address.

Commonly Asked Questions

What is the difference between built-in email validation and email

validation regex?

Angular 8 provides built-in email validation using the EmailValidator class.
This class checks if an email address is valid based on the format of the
email address and the presence of the "@" symbol. Email validation regex, on
the other hand, provides more customizable validation criteria. You can use
email validation regex to check for the existence of the domain name, the
length of the domain name, and other criteria.

How do I test my email validation regex?

You can test your email validation regex using online regex testers such as
regex101 and regexr. These tools allow you to enter a test email address and
see if it matches your regex pattern.

What are some common email validation regex patterns?

Some common email validation regex patterns include:

  • /^\w+([\.-]?\w+)@\w+([\.-]?\w+)(\.\w{2,3})+$/ - checks for a valid email address format
  • /[1]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/ - checks for a valid email address format and the existence of the domain name
  • /[\s@]+@[\s@]+\.[\s@]{2,}$/ - checks for a valid email address format and the presence of the "@" symbol and the domain name

These patterns can be modified to suit your specific email validation needs.


  1. a-zA-Z0-9._%+- ↩︎