Good email regex?


#1

Does anyone have a good email regex that works in Ionic? Because I am struggling to find one. This is my current one:

^[a-zA-Z0-9._]+[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$

but it accepts some wrong stuff like whatever@gmail


#3

I’ve all the validation control on a different service. This is my function

// SINGLE FIELD VALIDATORS

export function emailValidator(control: FormControl): {[key: string]: any} {
  var emailRegexp = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
  if (control.value && !emailRegexp.test(control.value)) {
    return { invalidEmail: true };
  }
}

#4

This is what i use:

let EMAIL_REGEXP = /^(([^<>()\[\]\\.,;:\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,}))$/;

#5

none of them works for me :open_mouth:
they don’t accept my correct address


#6

how is this working for you?

I have tested it here:
http://regexr.com/
and it doesn’t work


#7

This seems to work fine for me up to now

Validators.pattern("[a-zA-Z0-9\.\_-]{1,}@[a-zA-Z0-9\.\_-]{1,}[a-zA-Z]{2,}")

#8

this one is worse than the one I have posted


#9


#10

this is the same regex as you posted? because it doesn’t look same


#11

Yep it’s the same copy pasted from here :slight_smile:


#12

but I am copy-pasting it there and it gives no match response

^(([^<>()\[\]\\.,;:\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,}))$

#13

could you please answer?


#14

Don’'t know what to tell you just tested again

^(([^<>()\[\]\\.,;:\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,}))$


#15

Can you please explain, in what way worse?


#16

what the hell, we are both testing the same regex, on same site, and we have different results? how is this possible


#17

lol maybe try to test inside your app or use another site ?
https://www.regextester.com/


#18

i have found out what was wrong on this site-testing
but still, it doesn’t work in my app, this is weird, i use it like this:

this.signupForm = formBuilder.group({
      email: ['', Validators.compose([Validators.pattern('^(([^<>()\[\]\\.,;:\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,}))$'), Validators.required])]
    });

#19

am i doing this right?


#20

Make a custom validator and use it in your page

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

export class EmailValidator {

   static isInvalidEmailFormat(control: FormControl){
        let EMAIL_REGEXP = /^(([^<>()\[\]\\.,;:\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,}))$/;

        if (control.value != "" && (control.value.length <= 5 || !EMAIL_REGEXP.test(control.value))) {
            return { "isInvalidEmailFormat": true };
        }

        return null;
    }

}

email     : ['', Validators.compose([EmailValidator.isInvalidEmailFormat, Validators.required])],

#21

with this i have an error that control.value is undefined

edit: nvm i have fixed this, and now to my surprise regex is working as well