webmaster1 Posted July 25, 2010 Share Posted July 25, 2010 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 More sharing options...
magnetica Posted July 25, 2010 Share Posted July 25, 2010 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> Link to comment https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/#findComment-1090868 Share on other sites More sharing options...
webmaster1 Posted July 25, 2010 Author Share Posted July 25, 2010 Did that work for you? I just get a blank page after copying and pasting your code. Link to comment https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/#findComment-1090889 Share on other sites More sharing options...
webmaster1 Posted July 25, 2010 Author Share Posted July 25, 2010 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> Link to comment https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/#findComment-1090891 Share on other sites More sharing options...
webmaster1 Posted July 25, 2010 Author Share Posted July 25, 2010 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> Link to comment https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/#findComment-1090909 Share on other sites More sharing options...
AbraCadaver Posted July 25, 2010 Share Posted July 25, 2010 $valid = filter_var($contactemail, FILTER_VALIDATE_EMAIL); // returns true or false Link to comment https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/#findComment-1090911 Share on other sites More sharing options...
webmaster1 Posted July 25, 2010 Author Share Posted July 25, 2010 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> Link to comment https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/#findComment-1090916 Share on other sites More sharing options...
AbraCadaver Posted July 25, 2010 Share Posted July 25, 2010 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 Link to comment https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/#findComment-1090921 Share on other sites More sharing options...
webmaster1 Posted July 26, 2010 Author Share Posted July 26, 2010 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> Link to comment https://forums.phpfreaks.com/topic/208829-quickie-email-validation-as-a-variable/#findComment-1091491 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.