ahphpnz Posted January 27, 2015 Share Posted January 27, 2015 Hey all, trying to figure out a logic flaw in this validation script. Basically if I use !preg_match or the preg_match examples (noted below), I do not get the correct results. My interpretation of preg_match and my current understanding of the logic is noted below (please correct me if I'm wrong). 1. If I use alternate attempt !preg_match, the regex would be compared against the "string" in the $email variable. If the regex expression matches, error will be thrown and script would stop. 2. If I use alternate attempt !preg_match, the condition would be not true and move to else where the string contained within $email variable would be run through the vld function. 3. If I maintain the current script using preg_match, if the string matches the match, the condition would be true and therefore string of $email variable runs through VLD function else cleanup and throw error... Are my statements above correct in this context? I think I'm pretty close to cracking it but seem to be missing something simple. The current script is referenced below, forgive me if I've lost the plot. Long hours attempting to understand this language, from what I've seen so far its great. Thanks in advance, A //Alternate attempt if (!preg_match("/^[^@]+@[^@.]+\.[^@]*\w\w/", $email)) { $error = "Please enter a valid email address"; } else { $email = vld($_POST['email']); } Current code that doens't work: if (empty($_POST['email'])) { $error = "Email is required"; } else { if (strlen($_POST['email']) < 5) { $error = "Email is too short!"; } else { if (strlen($_POST['email']) > 30) { $error = "Email is too long!"; } else { if (preg_match("/[\(\)\<\>\,\;\:\\\"\[\]]/", $email)) { $error = "The email address contains illegal characters"; } else { if (preg_match("/^[^@]+@[^@.]+\.[^@]*\w\w/", $email)) { $email = vld($_POST['email']); } else { $error = "Please enter a valid email address"; } } } } }} Quote Link to comment https://forums.phpfreaks.com/topic/294257-logic-issue-email-validation/ Share on other sites More sharing options...
cyberRobot Posted January 30, 2015 Share Posted January 30, 2015 Have you considered using PHP's built-in method for validating email addresses? More information can be found here: http://php.net/manual/en/filter.examples.validation.php 1 Quote Link to comment https://forums.phpfreaks.com/topic/294257-logic-issue-email-validation/#findComment-1504350 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.