0

I am having problem in validating email address with regular expression.The code is seems to works fine until user put some long characters separated by(.dot) at end.Code is

if(preg_match('~^\b[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-z]{2,4}+\b$~',$_POST["email"]))

{
    $valid ="valid";
}
else
{
    $valid ="invalid";
}

Problem comes when user put something like ksdlk@gll.lik.ij.lio.lk.gr it returns "valid". How to deal with last characters, that would only allow 4,5 characters from @ like ".com"or ".co.uk".

Thanks in advance

reuben
  • 3,360
  • 23
  • 28
user1187405
  • 463
  • 2
  • 6
  • 17

1 Answers1

1

Try to use this

if(filter_var('email@example.com', FILTER_VALIDATE_EMAIL))return TRUE;
else return FALSE;
denil
  • 690
  • 5
  • 8