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
--> errorname@doma
--> no errorname@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.
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.
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.
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.
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.
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])?)*$/
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,
I use this:
/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With