How to format and validate email node js


I would like that email had format like: a@b.c.

Which is the best way to do it?

I have a component for registration and I have the field like this:

              <input matInput placeholder="Email" name="email" [(ngModel)]="email" required>

In my usersRouter I have the function for registration:'/users/register', (req, res) => {
        const user = new User({

Also, I use mongo and in the UserSchema I have this for the email:

email: {
        type: String,
        required: true



Use regular expression something like that:

Solution 1:


Sample code:

const emailToValidate = '';
const emailRegexp = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;


Solution 2:

Because you use angular, you are able to validate the email on front-end side by using

If you check angular source code of here, you will find an EMAIL_REGEXP const variable with the following value:


You could use it on back-end side too, to validate the input.

Answered By – gr4viton

Answer Checked By – Pedro (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.