ibda12u Posted January 14, 2007 Share Posted January 14, 2007 Hello just as my title says, I have a form with an email field that is optional for the user to fill out.What I've been trying to do is if a user fills out the optional email address field, it will check that field to make sure there is a . and an @ in the email.My code here works, except it won't validate if the email field is empty.Any idea's?[code] <?php if($fathers_email != "" && !strstr($fathers_email,"@") || !strstr($fathers_email,".")) {echo 'Sorry all required fields were not filled out, please <a href="javascript: history.go(-1)">go back</a> and correct Father\'s Email Address. ';} else {echo ' [/code] Quote Link to comment https://forums.phpfreaks.com/topic/34169-optional-email-field-validate-if-not-empty/ Share on other sites More sharing options...
Philip Posted January 14, 2007 Share Posted January 14, 2007 try:[code]<?phpif(isset($fathers_email) {// code for if there IS an email (validation goes here)} else {// code for if there is NO email}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34169-optional-email-field-validate-if-not-empty/#findComment-160744 Share on other sites More sharing options...
ibda12u Posted January 14, 2007 Author Share Posted January 14, 2007 I tried this, and it won't let me not enter an email at all.[code] <?php if(isset($fathers_email)){ if(!strstr($fathers_email,"@") || !strstr($fathers_email,".")) {echo '<span class="style5">Sorry all required fields were not filled out, please <a href="javascript: history.go(-1)">go back</a> and correct Father\'s Email Address.</span> ';}else {echo ' [/code] Quote Link to comment https://forums.phpfreaks.com/topic/34169-optional-email-field-validate-if-not-empty/#findComment-160785 Share on other sites More sharing options...
Philip Posted January 14, 2007 Share Posted January 14, 2007 What's the rest of the code? You keep cutting it off at echo ' Quote Link to comment https://forums.phpfreaks.com/topic/34169-optional-email-field-validate-if-not-empty/#findComment-160801 Share on other sites More sharing options...
ibda12u Posted January 14, 2007 Author Share Posted January 14, 2007 it just displays the pages html[code] <?php if($fathers_email != "" && !strstr($fathers_email,"@") || !strstr($fathers_email,".")) {echo 'Sorry all required fields were not filled out, please <a href="javascript: history.go(-1)">go back</a> and correct Father\'s Email Address. ';} else {echo 'Enrollment Application Step 5</font></b></font><p><font face="Verdana" size="1">Please complete the form below to reserve your child\'s spot. While it does not guarantee that an opening will be immediately available, it does promise that the next available spot will be held for your child. <font color="#CC3300">To avoid delays </font></font> ';} ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34169-optional-email-field-validate-if-not-empty/#findComment-160811 Share on other sites More sharing options...
Philip Posted January 14, 2007 Share Posted January 14, 2007 [code]<?phpif(isset($fathers_email) { if(!strstr($fathers_email,"@") || !strstr($fathers_email,".")) { echo "Incorrect email address formatting!"; } else { echo 'Enrollment Application Step 5</font></b></font><p><font face="Verdana" size="1">Please '; echo 'complete the form below to reserve your child\'s '; echo 'spot. While it does not guarantee that an opening will be '; echo 'immediately available, it does promise that the next available '; echo 'spot will be held for your child. <font color="#CC3300">To '; echo 'avoid delays </font></font>'; }} else {echo 'Sorry all required fields were not filled out, please <a href="javascript: history.go(-1)">go back</a> and correct Father\'s Email Address. ';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34169-optional-email-field-validate-if-not-empty/#findComment-160818 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.