Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 reactive form Email validation by regular expression fail

I'm using a reactive form to get user input. Not satisfied with the EmailValidator I'm using a pattern.

emailRegEx = '^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$';
this.email = new FormControl('', [Validators.required, Validators.pattern(emailRegEx)]);

And the HTML:

<input type="email" formControlName="email" [ngClass]="contactForm.get('email').errors && (contactForm.get('email').dirty || isButtonClicked) ? 'contact-input input-error' : 'contact-input'">

But here's the thing, for some reason the regex is accepting 4 chars after the @, without a period. name@d --> error
name@doma --> no error
name@domain. --> error
[email protected] --> no error

I checked this regex in multiple online regex testers, and they all only accept the last example above, none of them accept the second one.

EDIT:
The regular expression is fine and works well, the problem is that somehow the pattern validator isn't parsing the regex correctly, or something.

like image 659
AvailableName Avatar asked Mar 08 '18 14:03

AvailableName


People also ask

How do I use reactive email validation in angular?

Email Validations are implemented in Reactive forms using in-built Angular directives. For Angular applications to use reactive forms, In app.module.ts, first import the FormsModule and ReactiveFormsModule modules. Let’s create a new component reactive-email-validation using the ng g component command.

How to validate emails created using the reactive forms approach?

To validate emails created using the reactive forms approach, we’ll follow these three steps: 1. Apply validation rules To access Angular’s built-in email validators, we need to use the Validators class. The validator function can process the value of a FormControl and determine whether the validation criteria has passed or not.

How do I create and validate a form in angular?

To create and validate form import FormGroup, FormBuilder and Validators modules from “@angular/forms” package. Insert FormBuilder in the constructor, also evoke the form object using FormGroup class. Inside the ngOnInit lifecycle hook, declare the form inside the group method.

What is custom pattern validation in angular?

Pattern validation: This allows you to specify a regular expression (regex) Angular email validation pattern that should match the user-provided value before validation can occur. Custom validation: You can also create your own custom email validators, especially if the built-in validators do not match your specific use case.


3 Answers

The pattern is not correct as a string. In deed you are inside a string so to escape '.' you need to use double backslash like:

emailRegEx = '^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$'

Or if you want to avoid doing so i suggest to use:

emailRegEx = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/

Edit: Keep in mind that this is a simple pattern that exclude a lot of valid email address (see RFC 5322 (sections 3.2.3 and 3.4.1) and RFC 5321). For example the angular build in email validator use the following pattern

/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[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])?)*$/
like image 121
JEY Avatar answered Oct 19 '22 19:10

JEY


You can use CustomValidator package that offers too much types of validation :https://www.npmjs.com/package/ng2-validation

import it like that :

import { CustomValidators } from 'ng2-validation';

and use it in your form control :

this.email = new FormControl('', [Validators.required, CustomValidators.email]);

Regards,

like image 33
Mohamed Ali RACHID Avatar answered Oct 19 '22 20:10

Mohamed Ali RACHID


I use this:

/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/ 
like image 1
vladernn Avatar answered Oct 19 '22 21:10

vladernn