Author Topic: having issues validating an email with textbook code  (Read 218 times)

DVANGELION

  • New Member
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
This is related to a homework assignment. I have some code from my PHP textbook that doesn't seem to work properly. The page asks for a name, email, subject and message, but it always echoes "\"$fieldName\" is not a valid e-mail address.<br />\n" from the else part of the validateEmail function. I think the issue is (and I don't entirely understand this part) "/^[\w-]+(\.[\w-]+)*@" . "[\w-]+(\.[\w-]+)*" . "(\.[[a-z]]{2,})$/i". I get that it's being compared to the string that was entered in the email field, but I don't know what it's specifically looking for. I hate asking for help, but if anyone could steer me in the right direction, I would be very grateful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  <
html>
   <
head>
    <
title>Contact Me</title>
   </
head>
   <
body>
   <?
php
   
function validateInput($data$fieldName
   {
      global 
$errorCount;
      if (empty(
$data)) {
 
	
	
 echo 
"\"$fieldName\" is a required field.<br/>\n";
           ++
$errorCount;
           
$retval "";
      } else { 
           
$retval trim($data);
           
$retval stripslashes($retval);
      }
      return(
$retval);
 }
 
 function 
validateEmail($data$fieldName) { //this is the function I'm currently stuck on I believe
      
global $errorCount;
      if (empty(
$data)) {
 
	
	
 echo 
"\"$fieldName\" is a required field.<br />\n";
           ++
$errorCount;
           
$retval "";
      } else { 
           
$retval trim($data);
           
$retval stripslashes($retval);
           
$pattern "/^[\w-]+(\.[\w-]+)*@" "[\w-]+(\.[\w-]+)*" "(\.[[a-z]]{2,})$/i";
           if (
preg_match($pattern$retval)==0) {
           echo 
"\"$fieldName\" is not a valid e-mail address.<br />\n";
                ++
$errorCount;
           }
      }
      return(
$retval);
 }
 
 function 
displayForm($Sender$Email$Subject
 
$Message) {
 
?>
 
<h2 style = "text-align:center">Contact Me</h2>
 <form name="contact" action="ContactForm.php"
      method="post">
 <p>Your Name: <input type="text" name="Sender" 
 value="<?php
      
echo $Sender?>" /></p>
 <p>Your E-mail: <input type="text" name="Email"
      value="<?php echo $Email?>" /></p>
 <p>Subject: <input type="text" name="Subject" 
 value="<?php
      
echo $Subject?>" /></p>
 <p>Message:<br />
 <textarea name="Message"><?php echo $Message;
      
?></textarea></p>
 <p><input type="reset" value="Clear Form" />&nbsp;
      &nbsp;<input type="submit" name="Submit"
      value="Send Form" /></p>
 </form>
 <?php
 
}
 
 
$ShowForm TRUE;
 
$errorCount 0;
 
$Sender "";
 
$Email "";
 
$Subject "";
 
$Message "";
 
 if (isset(
$_POST['Submit'])) {
      
$Sender validateInput($_POST['Sender'],"Your Name");
      
$Email validateEmail($_POST['Email'],"Your E-mail");
      
$Subject validateInput($_POST['Subject'],"Subject");
      
$Message validateInput($_POST['Message'],"Message");
 
	
 
      if (
$errorCount==0)
           
$ShowForm FALSE;
      else
           
$ShowForm TRUE;
 }
 
 if (
$ShowForm == TRUE) {
      if (
$errorCount>0)
           echo 
"<p>Please re-enter the form information below.</p>\n";
      
displayForm($Sender$Email$Subject$Message);
 }
 else {
      
$SenderAddress "$Sender <$Email>";
      
$Headers "From: $SenderAddress\nCC: $SenderAddress\n";
      
$result mail("[your email here]"$Subject$Message$Headers);
      if (
$result)
           echo 
"<p>Your message has been sent. Thank you, " $Sender ".</p>\n";
      else
           echo 
"<p>There was an error sending your message, " $Sender ".</p>\n";
 }
  
?> 
  </body>
 </html> 


PHP Help Forum

having issues validating an email with textbook code
« on: February 16, 2017, 03:10:21 am »


Strider64

  • Professional PHP Helper
  • Senior Member
  • *
  • Posts: 946
  • Karma: 77
  • Don't Use mysql....use mysqli or PDO!
    • View Profile
    • Pepster's Place
Why not just use PHP built-in-function?
1
2
3
 if (!filter_var($emailFILTER_VALIDATE_EMAIL)) {
     
/* Invalid Email */
 
}



plus HTML5 has it where it check's an email address on the client's side.
« Last Edit: February 16, 2017, 05:58:39 am by Strider64 »
Insanity: doing the same thing over and over again and expecting different results -> https://www.pepster.com

PHP Help Forum

Re: having issues validating an email with textbook code
« Reply #1 on: February 16, 2017, 05:57:04 am »



PHP Help Forum

Re: having issues validating an email with textbook code
« Reply #2 on: February 16, 2017, 09:21:45 am »