bubblybabs Posted January 3, 2007 Share Posted January 3, 2007 Hello,I have searched the net and this forum but can't seem to find what I need...I would like to have multiple email text boxes that will allow the unused boxes remain blank but check that any entered emails are in the correct format... Right now what I have causes an error because it's trying to force all of the boxes to have an entry... what I'm using is:function validEmail($address){ if (eregi("^[a-z0-9]([\._\-]?[a-z0-9])*@[a-z0-9]([\.\-]?[a-z0-9])*\.[a-z]{2,}$", $address)) return true; else return false;}How do I make it so that a box may be blank but also check the format if there is something entered?Thank you,Babs Link to comment https://forums.phpfreaks.com/topic/32634-text-box-how-to-allow-it-to-remain-empty-but-also-check-email-format/ Share on other sites More sharing options...
papaface Posted January 3, 2007 Share Posted January 3, 2007 Basically all you need to do is use the isset() function. If something is entered run your function. Link to comment https://forums.phpfreaks.com/topic/32634-text-box-how-to-allow-it-to-remain-empty-but-also-check-email-format/#findComment-151835 Share on other sites More sharing options...
corbin Posted January 3, 2007 Share Posted January 3, 2007 Hehe someone beat me to it, but heres a farther explanation.You could do if(isset($var) && !validEmail($var)) {//error}Example:[code=php:0]<?function validEmail($address){ if (eregi("^[a-z0-9]([\._\-]?[a-z0-9])*@[a-z0-9]([\.\-]?[a-z0-9])*\.[a-z]{2,}$", $address)) return true; else return false;}if($_POST) {$emails = $_POST['email'];$i = 1;foreach($emails as $k=> $v) {if(strlen($v) > 0 && !validEmail($v)) {$error .= "Email{$i} is invalid.<br>";}$i++;}}if(!$_POST || $error) {form($error);}function form($error = '') {?><font color="red"><?=$error;?></font><form action="" method="post">Email1: <input type="text" name="email[]"><br>Email2: <input type="text" name="email[]"><br>Email3: <input type="text" name="email[]"><br>Email4: <input type="text" name="email[]"><br>Email5: <input type="text" name="email[]"><br></input type="submit" value="Submit"></form><?}?>[/code] Link to comment https://forums.phpfreaks.com/topic/32634-text-box-how-to-allow-it-to-remain-empty-but-also-check-email-format/#findComment-151839 Share on other sites More sharing options...
bubblybabs Posted January 3, 2007 Author Share Posted January 3, 2007 Many thanks for trying to point me in the right direction...This is what I tried:if (isset($_POST['to_email1'])) $_SESSION['to_email1'] = $_POST['to_email1']; else $_SESSION['email1'] = '';with the form validation that I have above...I still get an error stating that it's not a valid email address...What am I doing wrong?Of note, I did try what you put up corbin but I got an error...Thanks,Babs Link to comment https://forums.phpfreaks.com/topic/32634-text-box-how-to-allow-it-to-remain-empty-but-also-check-email-format/#findComment-151960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.