Jump to content

Quickie: Email validation as a variable?


webmaster1

Recommended Posts

The objective

If the email field of my form doesn't validate I want to set a variable to be true that then prompts and if/else statement to echo the error message.

 

The problem

All tutorials use a function to achieve this. I don't want to.

 

The code

 

This is the code I'm trying to implement:

<?php
$contactemail="notanemailatnotadomaindotcom";
if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $contactemail)) 
{
   return FALSE;
  $contactemail = FALSE; //This is my own addition.
}
?>
<ul>
<?php
if (!$contactemail){
                echo "<li class='fail'><label for='contactsubjecterror'> </label>↓ You did not input a valid email address.</li>";
            }
?>
</ul>

 

The question

Without having to use a function how do I validate my email field by outputing a TRUE/FALSE variable and without having to use a function?

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/
Share on other sites

return FALSE;

 

Can only be used by functions!

 

Best way here (because you want to precedural program)

 

<?php
$contactemail="notanemailatnotadomaindotcom";
    if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $contactemail)) 
    {
        $contactemail_validated = FALSE; //This is my own addition.
    }
    else{
        $contactemail_validated = TRUE; //email is valid
     }
?>
<ul>
<?php
     if ($contactemail_validated == FALSE){
                echo "<li class='fail'><label for='contactsubjecterror'> </label>↓ You did not input a valid email address.</li>";
            }
?>
</ul>

Whether I use a valid email address or not, I never end up with a FALSE result:

 

<?php
$contactemail="bla";
    if (eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $contactemail)) 
    {
        $contactemail_validated = FALSE; //This is my own addition.
    }
    else{
        $contactemail_validated = TRUE; //email is valid
     }
?>
<ul>
<?php
     if ($contactemail_validated == FALSE){
                echo "<li class='fail'><label for='contactsubjecterror'> </label>↓ You did not input a valid email address.</li>";
            }
else{echo $contactemail;}
?>
</ul>

 

erregi is deprecated. I'm having the same problem with preg_match.

 

If anyone could copy and paste the below code and point out the error I'd  grateful.

 

<?php
$contactemail="invalidemail";
    if (preg_match('/^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]/', $contactemail)) 
    {
echo"Validation FALSE!";
        $contactemail_validated = FALSE; //email is not valid
    }
    else{
echo"Validation TRUE!";
        $contactemail_validated = TRUE; //email is valid
     }
?>
<hr>
<?php
     if ($contactemail_validated == FALSE){
                echo "You did not input a valid email address!";
            }
     else{echo $contactemail;}
?>
<hr>

 

 

Brilliant, thanks! The working code:

 

<?php
//$contactemail="[email protected]"
$contactemail="invalidemail";
$valid = filter_var($contactemail, FILTER_VALIDATE_EMAIL); // returns true or false

// Notes:
// http://www.phpfreaks.com/forums/index.php/topic,305419.0.html
// http://www.w3schools.com/php/filter_validate_email.asp
?>
<hr>
<?php
     if ($valid == FALSE){
                echo "The following email is invalid: ".$contactemail;
            }
     else{echo "The following email is valid: ".$contactemail;}
?>
<hr>

That will work fine since you are comparing to false, but I pasted the wrong code.  The filter_var() doesn't return true, it returns the data you passed to it on success.  So to make my comment // returns true or false correct, use this:

 

$valid = (bool)filter_var($contactemail, FILTER_VALIDATE_EMAIL); // returns true or false

Thanks for checking back. It works fine:

 

<?php
//$contactemail="[email protected]"
$contactemail="invalidemail";
$valid = (bool)filter_var($contactemail, FILTER_VALIDATE_EMAIL); // returns true or false

// Notes:
// http://www.phpfreaks.com/forums/index.php/topic,305419.0.html
// http://www.w3schools.com/php/filter_validate_email.asp
?>
<hr>
<?php
if ($valid == FALSE){
echo "The following email is invalid: ".$contactemail;
}
else{
echo "The following email is valid: ".$contactemail;
}
?>
<hr>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.