1

I want to restrict duplicate words in an email using regex.

Currently I am using below regex that also restrict 2 consecutive dots (ex: a@gmail..com)

regexp: /A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z][{a-z},0-9](?:[a-z0-9-]*[a-z0-9]){1-2}?)$/i,

Now I want to also restrict duplicate words in email for example :-

abc.xyz@gmail.com.com

It should not accepted second .com .

Anand Systematix
  • 632
  • 5
  • 15
  • 4
    What??!! Why?? If you want to check if the email is valid, then **stop the regex madness** and just send them a confirmation email! Besides, this additional rule is wrong -- here is a perfectly valid domain: [tickets.tickets](http://tickets.tickets/). So for example, `admin@tickets.tickets` could be a perfectly valid email address. – Tom Lord Jan 04 '17 at 11:20
  • 2
    Honestly, I'm tempted to write something along the lines of [this](http://stackoverflow.com/a/1732454/1954610) in response to these "validate email addresses with a regex" questions ;) – Tom Lord Jan 04 '17 at 11:22
  • 1
    How does this stop me from using something like Another@fake.email or No@Email.Provided? Stop over complicating things, if you want to check the email address how about sending and checking? – krisph Jan 04 '17 at 11:38
  • Do you know that `com.com` exists? – Toto Jan 04 '17 at 12:28

1 Answers1

0

What about:

email="hello.world.world";
email=email.split(".");
fail = email.some((ae,ai)=>email.some((be,bi)=>bi==ai?false:ae==be));
//loop trough all pairs, if they are at the same index skip ( we dont want to check email[0]==email[0]), else compare, if same fail.
if(fail){alert("Wrong email");}
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151