Jump to content

PHP Form e-mail validation


JimKI

Recommended Posts

I need to know where to put an e-mail validation code in my form so it can check to see if valid e-mail I have tr5ied to place it but nothing seems to work I have attached the form code could some let me know where the validation code would go thanks jimhere is the validation codefunction 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; }Form Code<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><META NAME="robots" CONTENT="noindex,nofollow"><link href="../../../styles/_e-learning.css" rel="stylesheet" type="text/css" /></head><body background="images/background.jpg"><div id="container" align="center"><a name="result"></a><div id="header"><p class="tenTitle">Exercise 1a: 10 Great Reasons To Be In Business</p></div><div id="main"><?php // SET VARIABLES AND ARRAYS $stepNumber = '1a'; $numQuestions = '10'; $questions = array(1 => '1. Be your own boss', '2. Ability to set your own hours and working conditions', '3. Hard work directly benefits you and your business', '4. Undertaking an exciting new venture', '5. Control over earning and growth potential', '6. Unlimited opportunities to challenge yourself and learn', '7. Chance to be creative and take risks', '8. Ability to fully use your skills, knowledge, and talent', '9. Desire financial independence', '10. Ability to work in the industry/field of your choice' ); $numOptions = 5; $options = array(1 => 'Strongly Agree', 'Agree', 'Neutral', 'Disagree', 'Strongly Disagree' ); // IF FORM WAS FILLED OUT, CALCULATE SCORE, ELSE SCORE EQUALS 0 $score[] = 0; for ($i = 1; $i <= $numQuestions; $i++) { if(isset($_POST['radio'.$i])){ if ($options[$_POST['radio'.$i]] == $options[1]) { $score[$i] = 5; } elseif ($options[$_POST['radio'.$i]] == $options[2]) { $score[$i] = 4; } elseif ($options[$_POST['radio'.$i]] == $options[3]) { $score[$i] = 3; } elseif ($options[$_POST['radio'.$i]] == $options[4]) { $score[$i] = 2; } else { $score[$i] = 1; } } else { $score[$i] = 0; } } // UNIQUE TO FIRST SESSION OF EACH STEP $_SESSION['totalScore'] = 0; // ADD QEUSTIONS AND ANSWERS TO $EMAIL_BODY AND $EMAIL_BODYCC BELOW // IF FORM WAS SUBMITTED, MOVE ON TO NEXT STEP (ERROR CHECKING) if(isset($_POST['submit'])) { // CHECK FOR MISSING DATA (ERROR CHECKING) for ($i = 1; $i <= $numQuestions; $i++) { if (empty($_POST['radio'.$i])) { $problem = TRUE; $dataSpan[$i] = '<span style="color:red; font-weight:bold;">'.$questions[$i].'</span>'; } else { $dataSpan[$i] = $questions[$i]; } } if (empty($_POST['email'])) { $problem = TRUE; $emailSpan = '<span style="color:red; font-weight:bold;">*Your email address</span>'; } else { $emailSpan = '*Your email address'; } if (!empty($_POST['emailCc']) && (empty($_POST['name']))) { $problem = TRUE; $nameSpan = '<span style="color:red; font-weight:bold;">Your Name</span>'; } else { $nameSpan = 'Your Name'; } // IF MISSING DATA, PREPARE TO DISPLAY FORM. IF NO MISSING DATA, MOVE ON TO NEXT STEP (THANK YOU, SEND EMAILS AND LINK TO NEXT FORM) if ($problem == TRUE) { $display = TRUE; } else { $display = FALSE; } } // IF THE FORM IS NOT BEING SUBMITTED (FIRST VISIT TO PAGE), SET THE // FORM DISPLAY VARIABLE TO TRUE AND DEFINE VALUES OF DATA#SPAN VARIABLES (QUESTIONS) else { $display = TRUE; for ($i = 1; $i <= $numQuestions; $i++) { $dataSpan[$i] = $questions[$i]; } $emailSpan = '*Your email address'; $nameSpan = 'Your Name'; } if ($display == TRUE) { print '<p><strong>Instructions</strong>: Below are some of the attitudes and desires that many successful entrepreneurs possess. Read the list carefully. Think about how each of the statements relate to you - your ideas, desires, motivations, and areas of comfort or discomfort. In this exercise, quickly check each category with the answer that best relates to you, then go back and think about each - perhaps you might change your mind with this further refection - you can always come back later and change your answer.</p>'; if ($problem == TRUE) { print '<p>Please fill in questions marked in <span style="color:red; font-weight:bold;">red</p>'; } print '<div id="tenForm"> <form action="ten_steps_'.$stepNumber.'.html#result" method="POST" > <table border="0" cellpadding="4" cellspacing="3">'; for ($i = 1; $i <= $numQuestions; $i++) { echo '<tr> <td align="left" valign="top" border="0" class="submitCell"> <p>'.$dataSpan[$i].'</p>'; for ($j = 1; $j <= $numOptions; $j++) { $elemID = "radio" . $i . "_" . $j; $elemName = "radio" . $i; $chk = ""; if(isset($_POST[$elemName])) { if($_POST[$elemName] == ((string) $j)) { $chk = "CHECKED"; } } echo "<input type=\"radio\" id=\"$elemID\" name=\"$elemName\" $chk value=\"$j\">$options[$j]\n"; } print '</td> </tr>'; } print '</table><table border="0" cellpadding="3" cellspacing="2"> <tr> <td align="left" valign="top" border="0">'; include ($filePath.'/e-learning/tenScores.html'); print '</th> </tr> <tr> <td align="left" valign="top" border="0"> Submitting the form (* = required) </th> </tr> <tr> <td class="submitCell"> '.$emailSpan.'<br /> (to receive a copy of this form)<br /> <input type="text" size="40" maxlength="100" name="email" value="'.$_POST['email'].'" /> </td> </tr> <tr> <td class="submitCell"> Send Copy To eMentor<br /> <input type="text" size="40" maxlength="100" name="emailCc" value="'.$_POST['emailCc'].'" /> </td> </tr> <tr> <td class="submitCell"> '.$nameSpan.'<br /> (*required if sending a copy)<br /> <input type="text" size="40" maxlength="100" name="name" value="'.$_POST['name'].'" /> </td> </tr> <tr> <td class="submitCell" align="center"> <input type="reset" value="Clear" /> <input type="submit" name="submit" value="Submit" /><br /> <a href="'.$filePathState.'about_privacy.html" target="_blank"><span style="font-size:9px;">View our privacy policy</span></a> </td> </tr></table></form></div>'; } else { // DEFINE SESSION VARIABLES $_SESSION['email']=$_POST['email']; $_SESSION['emailCc']=$_POST['emailCc']; $_SESSION['name']=$_POST['name']; $_SESSION['score'.$stepNumber]=array_sum($score); $_SESSION['totalScore'] = $_SESSION['totalScore'] + $_SESSION['score'.$stepNumber]; // DEFINE VARIABLES FOR MAILING TO USER AND BUZGATE $to = 'askbuz@buzgate.org,'.$_POST['email']; $email_subject = "Your Results: 10 Great Reasons To Be In Business"; for ($i = 1; $i <= $numQuestions; $i++) { $eBodyQuestions .= $questions[$i]."\n" .$options[$_POST[radio.$i]]."\n\n"; } $email_body = $eBodyQuestions."Score: ".array_sum($score)."\n INTERPRETING THE SCORE:\n Score of 34-50: High Match\n Score of 21-35: Medium Match\n Score of 0-20: Low Match\n Total Score: ".$_SESSION['totalScore']."\n\n"; $headers = "From:".$_POST['email']; //PLACE VARIABLES IN MAIL FUNCTION mail($to, $email_subject, $email_body, $headers); // DEFINE VARIABLES FOR MAILING TO USER AND BUZGATE $toCc = $_POST['emailCc']; $email_subjectCc = "Your Results: 10 Great Reasons To Be In Business from ".$_POST['name']; $email_bodyCc = $_POST['name']." has sent this to you from www.BUZGate.org/8.0/".$state_abbrv_low."/".$page_name."\n Please contact us at askbuz@buzgate.org if you have received this in error.\n\n"; $email_bodyCc = $email_bodyCc.$email_body; $headersCc = "From:askbuz@buzgate.org"; //PLACE VARIABLES IN CC MAIL FUNCTION mail($toCc, $email_subjectCc, $email_bodyCc, $headersCc); // DISPLAY RESPONSE TO CORRECTLY FILLING OUT FORM echo "<p class='textCenter' >Thank you ".$_POST['name']." for completing Step ".$stepNumber."</p> <p class='textCenter'>Your Step ".$stepNumber." Score: ".$_SESSION['score'.$stepNumber]."<br /> Your Total Score: ".$_SESSION['totalScore']."</p>"; include ($filePath.'/inc2/tenScores.html'); echo "<p class='textCenter'><a href='ten_steps_1b.html'>Click here</a> to continue to 1b: 10 Great Reasons Not To Be In Business</p>" ; }?></div> <div id="footer"></div></div></body></html>

Link to comment
Share on other sites

Instead of

if (empty($_POST['email'])) {$problem = TRUE;$emailSpan = '<span style="color:red; font-weight:bold;">*Your email address</span>';}else {$emailSpan = '*Your email address';}

do this

if (empty($_POST['email'])) {  $problem = TRUE;  $emailSpan = '<span style="color:red; font-weight:bold;">*Your email address</span>';}else {  if (!check_email_address($_POST['email'])) {    $problem = TRUE;    $emailSpan = '<span style="color:red; font-weight:bold;">*Your email address</span>';  } else {    $emailSpan = '*Your email address';  }}

Link to comment
Share on other sites

  • 2 weeks later...
Instead of
if (empty($_POST['email'])) {$problem = TRUE;$emailSpan = '<span style="color:red; font-weight:bold;">*Your email address</span>';}else {$emailSpan = '*Your email address';}

do this

if (empty($_POST['email'])) {  $problem = TRUE;  $emailSpan = '<span style="color:red; font-weight:bold;">*Your email address</span>';}else {  if (!check_email_address($_POST['email'])) {    $problem = TRUE;    $emailSpan = '<span style="color:red; font-weight:bold;">*Your email address</span>';  } else {    $emailSpan = '*Your email address';  }}

This did not help it still allowed me to put anything in there and go to next screen without a warning
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...