aboutus aboutus - 4 days ago 4
Javascript Question

Regex to don't allow dots in email at wrong position

I have to validation emails from a company. The regex should validate if it come from the company and could come from John.

My regex looks like this now:

/[a-z0-9.]*(john)[a-z0-9.]*@mycompany\.com/


The only problem, it's allows dots in wrong place.
I see valid this emails:

john@mycompany.com
smith.john@mycompany.com
john.smith@mycompany.com


But i shouldn't see valid these:

john.@mycompany.com
.john@mycompany.com

Jay Jay
Answer

/^([a-z0-9][a-z0-9.]*)?(john)([a-z0-9.]*[a-z0-9])?@mycompany\.com$/

This should make sure that characters before/after (john) don't start/end with a . respectively.