0

I'm working on a regex for email verification.

This is what I have so far:

(?!^.*([._%+$-])\1+.*$)^[a-zA-Z0-9][a-zA-Z0-9._%+$-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,7}$

I've gotten it to fulfill all these requirements:

  1. Consecutive periods (note, multiple periods can occur in address, they have to be separated by valid characters). Example customer@yahoo..com
  2. User names starting with a special character. Example #customer@yahoo.com
  3. User names with two special characters in a row. Example cu$$tomer@yahoo.com
  4. Suffix name minimum length is 2. Example of invalid length: customer@yahoo.c
  5. Suffix maximum length is 7. Example of invalid length: customer@yahoo.commmmmmm

But I am stuck on this last one:

  1. User name special character cannot follow period. Example customer.name.#name2@yahoo.com

I've attempted to add a second negative lookahead like this but I can't get it to work correctly.:

(?!^.*([._%+-])\1+.*$)(?!^.*(.)\2+([._%+-])$)^[a-zA-Z0-9][a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,7}$

Any help is MUCH appreciated!

kelsny
  • 23,009
  • 3
  • 19
  • 48
Aroff
  • 1
  • 1
  • If you achieved all that, what exactly is holding you back in adding this additional requirement? Your regex already has negative look ahead assertions... – trincot Mar 17 '23 at 20:41
  • 2
    "regex for email verification" - famous last words, besides, isn't this a dupe of https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression or is this some sort of self-imposed challenge? – kelsny Mar 17 '23 at 20:56
  • @vr. : I don't know if it's a challenge but the email format is customized. – Casimir et Hippolyte Mar 17 '23 at 21:03
  • @CarySwoveland What does this have to do with Ruby? – Unmitigated Mar 18 '23 at 01:39
  • Unfortunately you didn't post samples. To modify your regex to do number 6. would be `^(?!.*([._%+-])\1+)(?![^@]*\.[_%+-])[a-zA-Z0-9][a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,7}$` – sln Mar 18 '23 at 22:16
  • @vr. not a self imposed challenge, requirements directly from the business unfortunately! – Aroff Mar 19 '23 at 17:52
  • @trincot Adding a second negative look ahead just wasn't working correctly. When I added it, it was either completely disabling/hiding the input box, or making it so that the proper formatting was giving me an error saying it was wrong. – Aroff Mar 19 '23 at 17:55
  • @sin sorry - not sure what you mean by samples? This is my first post here, so I apologize if my post is missing info! But thank you for your response - I will give this a shot! – Aroff Mar 19 '23 at 17:56

2 Answers2

0

This regex should do what you want

/^(?!^.*([._%+-])\1+.*$)(?!^[^@]*\.[^@]*[._%+-])[a-zA-Z0-9][a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,7}$/;

const emailRegex = /^(?!^.*([._%+-])\1+.*$)(?!^[^@]*\.[^@]*[._%+-])[a-zA-Z0-9][a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,7}$/;

// Test valid email addresses
console.log(emailRegex.test('customer@yahoo.com')); 
console.log(emailRegex.test('customer.name@yahoo.com')); 
console.log(emailRegex.test('customer_name@yahoo.com')); 

console.log("---------------------")

// Test invalid email addresses
console.log(emailRegex.test('customer@yahoo..com'));
console.log(emailRegex.test('#customer@yahoo.com')); 
console.log(emailRegex.test('cu$$tomer@yahoo.com')); 
console.log(emailRegex.test('customer@yahoo.c')); 
console.log(emailRegex.test('customer@yahoo.commmmmmm')); 
console.log(emailRegex.test('customer.name.#name2@yahoo.com')); 
cqb13
  • 96
  • 5
  • This was ALMOST it! Something made it so that there couldn't be two periods even when non consecutive (customer.first.last@yahoo.com gave an error). Thank you for your time, anyhow, I really appreciate it. – Aroff Mar 20 '23 at 15:55
0

You can try this:

^([[:alpha:]]+)(.?)([[:alpha:]]*)(\.([[:alpha:][:digit:]]*))?@[[:alpha:]]*\.[[:alpha:]]{3,7}\b

See: https://regex101.com/r/4IYjU9/1

4b0
  • 21,981
  • 30
  • 95
  • 142