0

Please read the requirement before thinking it is duplicate of any similar question.

As far as I found more than 20 Email validation regular expressions But all of them allows

a@a.co.in.in.in.in.in.in.in

as valid email address!!!

I want to restrict that only two dots(.) should be allowed.

For Example:

Valid Emails

somename@yahoo.in

somename@yahoo.co.in

somename@somesite.bizz

somename@gmail.com

somename@somesite.net

Invalid Emails

@.co

@a.com

a@a

a@.com

a@a.co.in.in //maximum two dots allowed otherwise invalid

a@a.co.in.in.in.in.in.in //maximum two dots allowed otherwise invalid

For this I tried with

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,5}[\.[a-zA-Z]{2,5}]{0,1}

It is working with 2 dots but throwing invalid message for

somename@somesite.com

Community
  • 1
  • 1
prem30488
  • 2,828
  • 2
  • 25
  • 57
  • This link does not contain solution for `allowing two dots"` – prem30488 May 09 '14 at 10:20
  • @ParthTrivedi, Take one of those solutions, then manually validate for two dots, as detailed in my answer below. – merlin2011 May 09 '14 at 10:21
  • 1
    Please read the question linked as duplicate. The problem isn't the answer, it's that you don't get it. Given `in.in` is a valid domain name, the owners can perfectly put a mailserver on the sub(sub(sub))domain `a.co.in.in.in.in.in.in.in`. That you don't see that as valid doesn't mean it isn't. If you want to count dots in a string, search this site for "count characters in string", but that has nothing to do with email validation. – CodeCaster May 09 '14 at 10:31
  • Probably you are right but my requirement is `Allow subdomain upto Only two dots`. I am sorry for that. – prem30488 May 09 '14 at 10:42
  • If you want to refuse people from `foo@mail.company.com`, then that's your loss. Also, you might want to escape the dot in your first character group. – CodeCaster May 09 '14 at 10:49
  • If this requirement will come I can change my reg ex to `[a-zA-Z0-9._%+-]+@[a-zA-Z0-9]+\.[a-zA-Z]+(\.[a-zA-Z]{2,5}){0,2}` – prem30488 May 09 '14 at 10:56

2 Answers2

1

I think that it should be like

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,5}(\.[a-zA-Z]{2,5}){0,1}

I'm not experienced in regex, but I think that [.[a-zA-Z]{2,5}] is considered character group. Parentheses do the trick.

kaiseroskilo
  • 1,719
  • 4
  • 21
  • 29
0

One possible approach is to first validate with one of the existing regular expressions that allows the email address a@a.co.in.in.in.in.in.in.in. If it validates, then do the following steps.

  1. Do a String split on the @ sign.
  2. Take the right hand side of the split, and do a split on the period .
  3. If the resulting array has length 2 or 3, then you have found a domain that satisfies your definition of a valid email. If not, then reject.

This way, you let regex you already found handle the grunt work, and you just check for the one additional condition you care about.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • But is it possible to do it in single reg ex? – prem30488 May 09 '14 at 10:26
  • @ParthTrivedi, It is absolutely possible, but as you saw in the linked question, email validation `regex` is already very hairy, so using an existing one that is known to work and then augmenting it this way tends to yield cleaner, more reliable code than trying to very carefully modify it for a special case. – merlin2011 May 09 '14 at 10:28