tags:

views:

75

answers:

3

Hello, I know it's my fault and it is not complicated but I cannot find the answer by myself so I beg you to help me build this RegEx.

I'm trying to avoid users to enter more than one email address by line, so I'd like to limit the "@" occurrences to only 1 and also check there are not commas (,)....

Here's a simplified version of the RegEx I'm already using to check the input....

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

How could I introduced a section to check there are not (@,) on the last part ?

Thanks in advance

+6  A: 

Instead of using a regex, you could split the string at the @, and count how many elements the output array has.

pavium
+3  A: 

Try just to add an ^ and one $

^\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$
Rubens Farias
Thanks a lot, Beaner's solution also did the trick...Now I'm still looking if I can avoid people to enter more than one subdomain, like [email protected]..... but even if cannot... now is good enough for me.....
A: 

This is what I am currently using. A quick test limited it to a single valid email for me.

@"^[a-zA-Z][\w\._%+-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
Beaner
Right offhand I can see that you aren't allowing a + before the @, so you aren't following the standard.
tloach
Writing a standards compliant regex for emails is... hard.
jonnii
tloach was correct. I edited my regex accordingly. Hopefully I will make it a little better every time I visit it. Code review is one of the things that makes stackoverflow valuable.
Beaner