anevins Posted June 9, 2011 Share Posted June 9, 2011 Hi, I'm using a function I took from the web for email validation, but there's something wrong with the form in general when I use this. When I test it out, I get the messages "Please enter a valid email address.Thank you..." when I input the letter 'o' into all input fields. I don't know why the validation isn't working for the "Thank you" message, but I think it has something to do with this email. Could anyone help me out here? function: function check_email_address($email) { // First, we check that there's one @ symbol, and that the lengths are right if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. return false; } // Split it into sections to make life easier $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) { if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { return false; } } if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) { return false; // Not enough parts to domain } for ($i = 0; $i < sizeof($domain_array); $i++) { if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { return false; } } } return true; } From the tutorial, this is how you're supposed to use the function: if (check_email_address($email)) { echo $email . ' is a valid email address.'; } else { echo $email . ' is not a valid email address.'; } The form: include_once('php/functions.php'); $firstname = $_POST['first_name']; $lastname = $_POST['last_name']; $email = $_POST['email']; $reason = $_POST['reason']; $i = 0; if(isset($_POST['submit'])){ if (empty($firstname)){ echo '<span class="error">Please enter your First name.</span>'; } if (empty($lastname)){ echo '<span class="error">Please enter your Last name.</span>'; } if (check_email_address($email)){ $i=1; } else { echo '<span class="error">Please enter a valid email address.</span>'; $i=0; } if (empty($reason)){ echo '<span class="error">Please enter your reason to contact me.</span>'; } if (empty($firstname) && empty($lastname) && ($i=0) && empty($reason)){ echo '<span class="error">All fields are required.</span>'; } if (!empty($firstname) && !empty($lastname) && ($i=1) && !empty($reason)){ echo '<span class="correct">Thank you...</span>'; } } ?> <div id="form"> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <table> <tr><td>First name:</td><td><input type="text" name="first_name" value="<?php echo $firstname; ?>"/></td></tr> <tr><td></td><td><?php echo $firstname_error; ?></td></tr> <tr><td>Last name: </td><td><input type="text" name="last_name" value="<?php echo $lastname; ?>"/></td></tr> <tr><td></td><td><?php echo $lastname_error; ?></td></tr> <tr><td>Email Address: </td><td><input type="text" name="email" value="<?php echo $email; ?>"/></td></tr> <tr><td></td><td><?php echo $email_error; ?></td></tr> <tr><td>Reason: </td><td><textarea rows="5" cols="16" name="reason" ><?php echo $reason; ?></textarea></td></tr> <tr><td></td><td><?php echo $reason_error; ?></td></tr> <tr><td> </td><td><input type="submit" name="submit" id="submit" value="submit" /></td></tr> </table> </form> </div> Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/ Share on other sites More sharing options...
xyph Posted June 9, 2011 Share Posted June 9, 2011 You want to use if/elseif/else here. Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227565 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 "o" is not a valid email address Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227568 Share on other sites More sharing options...
xyph Posted June 9, 2011 Share Posted June 9, 2011 His issue is the script continues to e-mail even though an invalid address was entered. Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227570 Share on other sites More sharing options...
anevins Posted June 9, 2011 Author Share Posted June 9, 2011 this gives same result: if (check_email_address($email)){ $i=1; } elseif (check_email_address($email)==false){ echo '<span class="error">Please enter a valid email address.</span>'; $i=0; } How am I supposed to use this elseif Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227571 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 His issue is the script continues to e-mail even though an invalid address was entered. just reread the op, didnt realize he was receiving both messages, if $i is still being set to 1 even though it is not a valid email, something must be wrong in the ereg() function used Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227595 Share on other sites More sharing options...
anevins Posted June 9, 2011 Author Share Posted June 9, 2011 could someone recommend another email validation function open source? Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227612 Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 here's a little something Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227615 Share on other sites More sharing options...
anevins Posted June 9, 2011 Author Share Posted June 9, 2011 I've used other email validation functions and I get the same problem. Does this mean there must be something wrong with the validation part: if (!empty($firstname) && !empty($lastname) && ($i=1) && !empty($reason)){ echo '<span class="correct">Thank you...</span>'; } Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227623 Share on other sites More sharing options...
anevins Posted June 9, 2011 Author Share Posted June 9, 2011 Problem solved (still using original function). The flag variables $i weren't working for some reason. New code statement: if (!empty($firstname) && !empty($lastname) && (check_email_address($email)) && !empty($reason)){ echo '<span class="correct">Thank you...</span>'; } Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227626 Share on other sites More sharing options...
xyph Posted June 9, 2011 Share Posted June 9, 2011 This logic is more sound if (empty($firstname)) echo '<span class="error">Please enter your First name.</span>'; elseif (empty($lastname)) echo '<span class="error">Please enter your Last name.</span>'; elseif (!check_email_address($email)){ echo '<span class="error">Please enter a valid email address.</span>'; $i=0; } elseif (empty($reason)) echo '<span class="error">Please enter your reason to contact me.</span>'; else echo '<span class="correct">Thank you...</span>'; Quote Link to comment https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227727 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.