Home > database >  RegEx for email so that it fails if the email does not have at least 1 letter somewhere before the @
RegEx for email so that it fails if the email does not have at least 1 letter somewhere before the @

Time:01-04

I have tried to find a solution to my problem here and here but without luck.
I have written these 2 RegExes for email validation which pretty meet almost all of my criteria
RegEx 1
^\w?[A-Za-z]{1}\w?@[a-z] .[a-z]{2,}(.[a-z]{2,}){0,1}$
RegEx 2
[\w.-] @[a-z] .[a-z]{2,}(.[a-z]{2,}){0,1}$

But they do not solve 1 critical issue.
I want the RegEx to fail when matched with something like:
[email protected]
_@gmail.com
8
@gmail.com
[email protected]
[email protected]
8
@gmail.com

So basically I want the email to have at least 1 lower or upper case letter and it does not matter it is at the beginning , middle or end of the email itself(before the @).

Could you please help me with this?

CodePudding user response:

/^[^@] [a-zA-Z]@[^@] $/

This regular expression uses the following elements:

^: This matches the start of the string.

[^@] : This matches one or more characters that are not the @ symbol.

[a-zA-Z]: This matches any single letter.

@: This matches the @ symbol.

[^@] : This matches one or more characters that are not the @ symbol.

$: This matches the end of the string.

strings would all be matched by this regular expression:

strings would not be matched:

CodePudding user response:

You could try:

/^[^@] [a-zA-Z]@[^@] $/

CodePudding user response:

You could use positive look ahead to do this.
(?=.*[a-z]) for lowercase
(?=.*[A-Z]) for uppercase

So something like this should do the trick:

(?=.*[a-z])(?=.*[A-Z])[^@ \t\r\n] @[^@ \t\r\n] \.[^@ \t\r\n] 

I let you customize the filter after the @ as you seem to have specific needs.

  • Related