2

I am trying to validate email in php using ereg, where I am not allowed to enter more than two dots after @ and it can't begin with any special character, how can I do it.

function chk($a)
{

$pattern = "^([A-Za-z0-9\.|-|_]{1,60})([@])";
$pattern .="([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,3})$";

  if (!@ereg($pattern, $a))
     return false;
  else
       return true;
}
Hunterr
  • 553
  • 1
  • 8
  • 28

3 Answers3

3

Please don't roll your own email validation.

if(filter_var($email, FILTER_VALIDATE_EMAIL) === true){
    return true;
} else {
    return false;
}
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
2
preg_match("/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/",'[email protected].');
Pratik Soni
  • 2,498
  • 16
  • 26
0
function custom_email_confirmation_validation_filter( $your_email ) {
   if(!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $your_email )){ 
        return 'invalid';
    }
    
  if( substr_count($your_email, '.') > 3){
    return 'invalid 1';
  }
  
  return 'valid';
}

echo custom_email_confirmation_validation_filter('[email protected]');