Jump to content

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="valid@email.com"
$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="valid@email.com"
$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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.