skip to Main Content

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 [email protected]
  2. User names starting with a special character. Example #[email protected]
  3. User names with two special characters in a row. Example [email protected]
  4. Suffix name minimum length is 2. Example of invalid length: [email protected]
  5. Suffix maximum length is 7. Example of invalid length: [email protected]

But I am stuck on this last one:

  1. User name special character cannot follow period. Example customer.name.#[email protected]

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!

2

Answers


  1. 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('[email protected]')); 
    console.log(emailRegex.test('[email protected]')); 
    console.log(emailRegex.test('[email protected]')); 
    
    console.log("---------------------")
    
    // Test invalid email addresses
    console.log(emailRegex.test('[email protected]'));
    console.log(emailRegex.test('#[email protected]')); 
    console.log(emailRegex.test('[email protected]')); 
    console.log(emailRegex.test('[email protected]')); 
    console.log(emailRegex.test('[email protected]')); 
    console.log(emailRegex.test('customer.name.#[email protected]')); 
    Login or Signup to reply.
  2. You can try this:

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

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search