Wednesday, 7 December 2016

How to Validate email address in PHP?


How to Validate email address in PHP?


Following are in built PHP function to validate the email address and Ip Address. See Examples below:
[email protected]';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo $email. ' - Invalid'; 
}else{
echo $email. ' - Valid';     
}

[email protected] - Valid

[email protected]';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo $email. '- Invalid'; 
}else{
echo $email. ' - Valid';     
}

[email protected] - Valid

$email='arunkumar.com';
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo $email. ' - Invalid'; 
}else{
echo $email. ' - Valid';     
}


arunkumar.com - Invalid



Question: How to Validate IP Address?
$ipAddress = '127.0.0.1';
if (!filter_var($ipAddress, FILTER_VALIDATE_IP)) {
    echo "$ipAddress is In-valid.";
}else{
    echo "$ipAddress is Valid.";
}

127.0.0.1 is Valid

$ipAddress = '127.test.0.0.1';
if (!filter_var($ipAddress, FILTER_VALIDATE_IP)) {
    echo "$ipAddress is In-valid.";
}else{
    echo "$ipAddress is In-Valid.";
}

127.test.0.0.1 is Valid

You must be using PHP>5.20

No comments :

Post a Comment