HTML pattern for email to a specific domain

Viewed 2123

I'm trying to find a regex pattern that only accepts email with a specific domain as follows.

z= [a-zA-Z0-9]

y= [a-zA-z]

(1) zzz.zzz@domain.y (eg: andrew.hugh@gmail.com, anne.dal@yahoo.ca)

(2) zzz.zzz@zzz.domain.y (eg: bryan.hawk@admin.msn.com, nick.deboer@info.school.edu)

Currently what I have so far is: [.a-zA-z0-9]*@domain.[a-zA-z0-9]

Could anyone help me on this?

2 Answers

Here you are for request no 1

^[a-zA-Z0-9]+@domain\.[a-zA-z]+$

for request no 2

^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.domain\.[a-zA-z]+$

It might be a lot easier to make a JavaScript function out of this, instead of doing it with Regex. As you have said in the comments; you don't mind.

function matchesEmail( email ) {

    var acceptedEmails = [ "msn.com", "school.edu" ];

    for( key in acceptedEmails ) {

        var acceptedEmailDomain = acceptedEmails[ key ];

        if( email.split( "@" )[1].split(".").slice(-2).join( "." ) === acceptedEmailDomain ) {

            return true;

        }

    }

    return false;

};

console.log( matchesEmail( "jesse@ddd.com" ) );
Related