0

I know this question asked many times but I could not find any solution to my specific validation.

I want to validate a username in email address to accept only letters, digits, underscore and DOT and NO dash (-) or any special character such as !#%&*()

Like this: aaa@aa.com, d123@ad.com, 22_dd@dd.com, dfd.df@ds.com

NOT like this: ss-ee@sd.com, fsd!@asd.com, 11-ee@sd.com

what i did:

if (!preg_match("/[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-zA-Z]+/", $email)) return("Invalid email address"); 

but it accepts dash.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ayman Hussein
  • 3,817
  • 7
  • 28
  • 48
  • possible duplicate of [How to validate an Email in PHP?](http://stackoverflow.com/questions/5855811/how-to-validate-an-email-in-php) – Phil Sturgeon Apr 08 '15 at 16:24

4 Answers4

1

You can do:

if (!preg_match('/^(?!.*?\.\.)[\w.]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]+$/', $email)) 
    return("Invalid email address"); 
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

To prevent 2 consecutive dots (.) use negative lookahead (?!.*?\.\.)

if (!preg_match('/^(?!.*?\.\.)[a-z0-9_.]+@[a-z0-9.-]+\.[a-z]+$/im', $email))
    return("Invalid email address"); 

EXPLANATION:

^             # Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed)
(?!           # Assert that it is impossible to match the regex below starting at this position (negative lookahead)
   .             # Match any single character that is NOT a line break character (line feed)
      *?            # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
   \.            # Match the character “.” literally
   \.            # Match the character “.” literally
)
[a-z0-9_.]    # Match a single character present in the list below
                 # A character in the range between “a” and “z” (case insensitive)
                 # A character in the range between “0” and “9”
                 # A single character from the list “_.”
   +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
@             # Match the character “@” literally
[A-Z0-9.-]    # Match a single character present in the list below
                 # A character in the range between “A” and “Z” (case insensitive)
                 # A character in the range between “0” and “9”
                 # The literal character “.”
                 # The literal character “-”
   +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\.            # Match the character “.” literally
[A-Z]         # Match a single character in the range between “A” and “Z” (case insensitive)
   +             # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\$             # Assert position at the end of a line (at the end of the string or before a line break character) (line feed)
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

You can use the following regex that will also prevent 2 consecutive dots in the username:

^(?!.*\.\..*@)[a-zA-Z0-9_.]+@\S+\.[a-zA-Z]+$

See demo here.

Details

  • ^ - start of string
  • (?!.*\.\..*@) - a negative lookahead that fails the match if there are two consecutive dots in the username part only
  • [a-zA-Z0-9_.]+ - one or more ASCII letters, digits or _ or .
  • @ - a @
  • \S+ - 1+ non-whitespace chars
  • \. - a dot
  • [a-zA-Z]+ - 1 or more ASCII letters
  • $ - end of string.

Sample code:

$re = "/^(?!.*\\.\\..*@)[a-zA-Z0-9_.]+@\\S+\\.[a-zA-Z]+$/m"; 
$str = "334345jtjert..j547j@ds.com\n334345jtjert.j547j@ds.com\nss-ee@sd.com\nfsd!@asd.com\n11-ee@sd.com"; 
preg_match($re, $str, $matches);
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
-2

You can use this:

[\\w\\-\\+\\&\\*]+(?:\\.[\\w\\-\\_\\+\\&\\*]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}
ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
lakshman
  • 656
  • 4
  • 18