How to validade email field

How can I validate an email field in ionic 2

It’s a very difficult problem, and I don’t think a public solution exists. But if you search StackOverflow you can find a lot of attempts that validate 95%+ of emails. Choose one you like.

I don’t think this is something worth attempting via a regex. The only thing that makes any sense to me is actually sending something to that email and having the recipient enter some sort of cookie that indicates that they received it. If you don’t want to bother doing that, then just accept anything entered.

I already tried on stackoverflow, and found nothing useful.
I just want to validate email as text input, not if the email exists or not

If you don’t care whether the email is deliverable, why do you care about validating anything about it?

import { FormControl } from '@angular/forms';
 
export class EmailValidator {
 
    static isValid(control: FormControl){
 
    	let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        let result = re.test(control.value);
        
        if (!result) {
        	return {
        		'email:validation:fail' : true
        	}
        }
        
        return null;
    }
 
}

Usage example:

import { EmailValidator } from '../../providers/email-validator';

this.signupForm = this.formBuilder.group({
      name: ['', Validators.required],
      email: ['', Validators.compose([Validators.required, EmailValidator.isValid])],
      password: ['', Validators.compose([Validators.required, Validators.minLength(6)])],
      password2: ['', Validators.compose([Validators.required, Validators.minLength(6)])]
    });
1 Like