How can I allow only 2 dots after @ in email validation php using ereg

2.4k views Asked by At

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;
}
3

There are 3 answers

5
cmorrissey On

Please don't roll your own email validation.

if(filter_var($email, FILTER_VALIDATE_EMAIL) === true){
    return true;
} else {
    return false;
}
3
Pratik Soni On
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].');
0
S.M. Golam Mostafa On
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]');