0

I tried the following regular expression to deny the user from entering email ID such as joooohn.smiiiithhh@xxxdddd.xxx

    @"^((?!\1{2,})([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))(?!\1{2,})@((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])(?!\1{2,})\.
([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])(?!\1{2,})\.
([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])(?!\1{2,})\.
(?!\1{2,})([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|([a-zA-Z]+[\w-]+\.)
(?!\1{2,})+(?!\1{2,})[a-zA-Z]{2,4})$"

But It didn't work. Can anyone help

Edit: 1. Not more than twice can a character appear.

RandomUser
  • 1,843
  • 8
  • 33
  • 65
  • will you please elaborate your question, means after how many repetitions it will give error and all. – Laxmikant Dange May 08 '14 at 11:49
  • 3
    /me wonders why such e-mails should be prohibited... – Ulugbek Umirov May 08 '14 at 11:50
  • 1
    what if I have e-mail registered in `.xxx` domain? – Ulugbek Umirov May 08 '14 at 11:52
  • what if my email address domain name is `google.com`? – ohaal May 08 '14 at 11:53
  • Two **oo** not a problem, .xxx can be a domain but I am asked to check that too.. requirements! – RandomUser May 08 '14 at 11:54
  • Why exactly would you ever want to do this? – Kendall Frey May 08 '14 at 11:54
  • 3
    Why bother? breee@zaaark.com might well be a perfectly valid email address. You will block potentially legitimate emails addresses. – Steve Pettifer May 08 '14 at 11:54
  • 9
    **1.** Email validation is a bad idea. **2.** Email validation [using a regex is a bad idea](http://stackoverflow.com/q/201323/7586). **3.** Limiting repeated characters is a bad idea, because those are valid emails. **4.** [Just check for `@` and `.`](http://programmers.stackexchange.com/a/78354/3677). Really. **5.** If people don't want to give you an email, you will not get their email. – Kobi May 08 '14 at 11:55
  • If a double o isn't a problem, then you need to be more specific about what you're trying to do. – Kendall Frey May 08 '14 at 11:55
  • 3
    Send email with validation link to given address. If validation link was called, email address is valid. No need for regex. – Corak May 08 '14 at 11:55
  • I updated the question with this point – RandomUser May 08 '14 at 11:56
  • @Kobi I agree with your point(s) thanks for being elaborate – RandomUser May 08 '14 at 11:56
  • 3
    There is nothing more frustrating than some system telling you that your email address isn't valid when you've been using it just fine for years – Damien_The_Unbeliever May 08 '14 at 11:57
  • For the record: [Here is my answer from 2009](http://stackoverflow.com/a/1000825/7586) for email validation in .Net - I'd do the same today. If the email is supported by my mail client, I'm good with it. – Kobi May 08 '14 at 12:17

2 Answers2

0

(\w)\1\1 should do the trick.

The \w matches any word character and puts it in the first capture group, then \1 checks for that same capture group, so this will match any string with three instances of a word character in a row. So any email which does NOT match this regex would be valid by your criteria.

As others have said, doing this validation is probably a bad idea, but if this is a requirement you've been given and can't avoid, the above solution will work. Alternatively you could use this to present the user with a warning, rather than reject the email altogether.

Ben Aaronson
  • 6,955
  • 2
  • 23
  • 38
  • 1
    Related: http://www.iiiiiiii.com/ (this isn't a counter example, just some music) – Kobi May 08 '14 at 12:04
  • 1
    @Kobi matches the pattern, as it should. Hopefully it's relatively clear from the last paragraph of my answer that this isn't an endorsement of blocking emails based on this pattern. But sometimes developers are given requirements that they have to implement whether or not they agree with them. And I could perhaps see value in it just placing an unobtrusive "Warning: this email contains repeated characters, please check that it is correct" next to the entry field- though it's not something I'd choose to do myself. – Ben Aaronson May 08 '14 at 12:04
  • "(this isn't a counter example, just some music)" Ah :) – Ben Aaronson May 08 '14 at 12:08
0

Bellow should work

^((.)\2{0,1}(?!\2))*$

st4hoo
  • 2,196
  • 17
  • 25