tronicsmasta Posted May 10, 2008 Share Posted May 10, 2008 Hey guys, found this code online and was wondering how to check the $isValid variable for true or false... I need to validate the users email basically i am using this format: if (!$_POST['email']) { echo "email field is empty"; $submitform == "no";} how can I apply that same logic to the output of this code: /** Validate an email address. Provide email address (raw input) Returns true if the email address has the email address format and the domain exists. */ function validEmail($email) { $isValid = true; $atIndex = strrpos($email, "@"); if (is_bool($atIndex) && !$atIndex) { $isValid = false; } else { $domain = substr($email, $atIndex+1); $local = substr($email, 0, $atIndex); $localLen = strlen($local); $domainLen = strlen($domain); if ($localLen < 1 || $localLen > 64) { // local part length exceeded $isValid = false; } else if ($domainLen < 1 || $domainLen > 255) { // domain part length exceeded $isValid = false; } else if ($local[0] == '.' || $local[$localLen-1] == '.') { // local part starts or ends with '.' $isValid = false; } else if (preg_match('/\\.\\./', $local)) { // local part has two consecutive dots $isValid = false; } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { // character not valid in domain part $isValid = false; } else if (preg_match('/\\.\\./', $domain)) { // domain part has two consecutive dots $isValid = false; } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { // character not valid in local part unless // local part is quoted if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $isValid = false; } } if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { // domain not found in DNS $isValid = false; } } return $isValid; } thanks! Link to comment https://forums.phpfreaks.com/topic/104986-email-validate-script/ Share on other sites More sharing options...
gnawz Posted May 10, 2008 Share Posted May 10, 2008 Hi, The first piece of code is checking if a user is trying to post an empty field just as one would do it in Javascript. The second set is checking for validity. I need to know what it is exactly you are asking. Link to comment https://forums.phpfreaks.com/topic/104986-email-validate-script/#findComment-537401 Share on other sites More sharing options...
tronicsmasta Posted May 10, 2008 Author Share Posted May 10, 2008 basically, when this this function is complete, i need to put $isValid in a if else statement to check its output so I can either continue with the script or output an error to the user... im still new to functions... thanks! Link to comment https://forums.phpfreaks.com/topic/104986-email-validate-script/#findComment-537538 Share on other sites More sharing options...
gnawz Posted May 12, 2008 Share Posted May 12, 2008 Then you should set a variable to check for email validity. Eg true if the email is valid and false if it is not. You can then put that in your code(Use the variable). Link to comment https://forums.phpfreaks.com/topic/104986-email-validate-script/#findComment-538795 Share on other sites More sharing options...
corbin Posted May 12, 2008 Share Posted May 12, 2008 Well the real regular expression for email addresses is like 14 pages long (a very slight exaggeration), but there are plenty out there that work just fine. Google gave me this link and it looks quite decent: http://www.regular-expressions.info/email.html Anyway, using the regex from there: $email = '[email protected]'; //an example email address if(preg_match('^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$', $email)) { echo 'valid!'; } else { echo 'invalid!'; } //or function form: function is_email($email) { return preg_match('', $email); } if(is_email($email)) { echo 'valid!'; } else { echo 'invalid!'; } [/code] Link to comment https://forums.phpfreaks.com/topic/104986-email-validate-script/#findComment-538804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.