aboutus
aboutus

Reputation: 239

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:

[email protected] [email protected] [email protected]

But i shouldn't see valid these:

[email protected] [email protected]

Upvotes: 0

Views: 874

Answers (4)

Martin Vyskočil
Martin Vyskočil

Reputation: 341

when I wrote my answer you already got many answers. :-) Ok, here is other what you can test.

^([[:alnum:]]+\.)?john(\.[[:alnum:]]+)?@mycompany\.com

I tested on regex101.com

If I understood well before and after john should be some word delimited by dot .

Upvotes: 0

user557597
user557597

Reputation:

Alternative solution

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

Upvotes: 1

Ilya Kushlianski
Ilya Kushlianski

Reputation: 958

Please try

\w+(john)?\.?\w+?@mycompany\.com

This matches

[email protected]
[email protected]
[email protected]

But not

[email protected]
[email protected]

In the last case it actually grabs only the part after the dot

[email protected]

Upvotes: 0

Jay
Jay

Reputation: 3950

/^([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.

Upvotes: 1

Related Questions