Jump to content

Problems validating email address


mattwal

Recommended Posts

Hello,

 

I'm having trouble validating an email via form value. The value should accept anything starting with a letter or a number and then continue with any combination letters numbers, the underscore, the period, and hyphen.

 

I've tried using a fake email for testing purposes [email protected] but it keeps throwing up error that the supplied email address is not valid....

 

Also I am not sure how to validate textarea's for possible harmful code. any help there would be appreciated also!

any help would be much appreciated. 

 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>FORM TO JOIN</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="css/main.css" />
<style type="text/css">
<!--

label,input {
        display: block;
        width: 150px;
        float: left;
        margin-bottom: 10px;
}

label {
        text-align: right;
        width: 75px;
        padding-right: 20px;
}

br {
        clear: left;
}

-->
</style>

</head>
<body>
<div id="container">
<div id="banner">
    	<img src="img/logo.jpg" height="100" width="700" title="Logo" /><!-- Logo banner for site -->
    </div>
    
    	<ul id="topnav">
        <!-- Add or remove pages from top navigation here -->
        	<li><a href="index.html">Home</a></li>
            <li><a href="about.html">About US</a></li>
            <li><a href="join.html">How To Join</a></li>
            <li><a href="members.html">Member List</a></li>
            <li><a href="contact.php">Contact US</a></li>
        </ul>
     
     <div id="content">
     
     <span class="heading">HOW TO JOIN -</span><!-- Page Title -->
     
     <h2 class="post-heading">Application to Join UBA</h2><!-- Post Title -->
     
     <!-- DO NOT edit below this line`unless you know what you are doing!!! -->

<p>Please complete the form below. Mandatory fields marked <em class="red">*</em></p>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<p>
<label for="company">Company:</label>
<input id="company" type="text" name="userCompany" maxlength="50" /><em class="red">*</em><br />

<label for="fname">First Name:</label>
<input id="fname" type="text" name="userFName" maxlength="25" /><em class="red">*</em><br />

<label for="lname">Last Name:</label>
<input id="lname" type="text" name="userLName" maxlength="25" /><em class="red">*</em><br />

<label for="address1">Address 1:</label>
<input id="address1" type="text" name="userAddress1" maxlength="100" /><em class="red">*</em><br />

<label for="address2">Address 2:</label>
<input id="address2" type="text" name="userAddress2" maxlength="100" /><br />

<label for="city">City:</label>
<input id="city" type="text" name="userCity" maxlength="25" /><em class="red">*</em><br />

<label for="state">State:</label>
<input id="state" type="text" name="userState" maxlength="25" /><em class="red">*</em><br />

<label for="zip">Zip:</label>
<input id="zip" type="text" name="userZip" maxlength="5" /><em class="red">*</em><br />

<label for="phone">Telephone:</label>
<input id="phone" type="text" name="userPhone" maxlength="12" value="e.x. 3305551234" /><em class="red">*</em><br />

<label for="fax">Fax:</label>
<input id="fax" type="text" name="userFax" maxlength="12" value="e.x. 3305551234" /><br />

<label for="email">Email:</label>
<input id="email" type="text" name="userEmail" maxlength="50" /><em class="red">*</em><br />

<label for="website">Website:</label>
<input id="website" type="text" name="userWebsite" maxlength="100" /><br />

<label for="hearfrom">How did you hear about us?</label>
<textarea id="hearfrom" type="text" name="userHearFrom"></textarea><br />

<label for="submit">Submit</label>
<input id="submit" type="submit" value="Submit Application!" /><br />
</p>
</form>

<?php
  /**
  * This function can be used to check the sanity of variables
  *
  * @access private
  *
  * @param string $type  The type of variable can be bool, float, numeric, string, array, or object
  * @param string $string The variable name you would like to check
  * @param string $length The maximum length of the variable
  *
  * return bool
  */

  function sanityCheck($string, $type, $length){

  // assign the type
  $type = 'is_'.$type;

  if(!$type($string))
    {
    return FALSE;
    }
  // now we see if there is anything in the string
  elseif(empty($string))
    {
    return FALSE;
    }
  // then we check how long the string is
  elseif(strlen($string) > $length)
    {
    return FALSE;
    }
  else
    {
    // if all is well, we return TRUE
    return TRUE;
    }
}


/**
  * This function if the $_POST vars are set 
  *
  * @access private
  *
  * return bool
  */
  function checkSet(){
  return isset($_POST['userCompany'], $_POST['userFName'], $_POST['userLName'], $_POST['userAddress1'], 
$_POST['userCity'], $_POST['userState'], $_POST['userZip'], $_POST['userPhone'], $_POST['userEmail'], 
$_POST['userWebsite']);
}

  /**
  * This function checks a number is greater than zero
  * and exactly $length digits. returns TRUE on success.
  *
  * @access private
  *
  * @param int $num The number to check
  * @param int $length The number of digits in the number
  *
  * return bool
  */
  function checkNumber($num, $length){
  if($num > 0 && strlen($num) == $length)
        {
        return TRUE;
        }
}

  /**
  * This function checks if an email address in a valid format
  *
  * @access private
  *
  * @param string $email The email address to check
  *
  * return bool
  */

function checkEmail($email){
  return preg_match('^[[:alnum:]][[:punct:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$^', $email) ? TRUE : FALSE;
}

  /**
  * This function checks if an Website address in a valid format
  *
  * @access private
  *
  * @param string $url The url address to check
  *
  * return bool
  */

function checkWebsite($url){
  return preg_match('^((http|https)://)?([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}(:alnum:]/+=%&_\.~?\-]*)$^', $url) ? TRUE : FALSE;
}




  // check all our variables are set
  if(checkSet() != FALSE)
        {
	// check the POST variable userCompany is sane, and is not empty
        if(empty($_POST['userCompany'])==FALSE && sanityCheck($_POST['userCompany'], 'string', 50) != FALSE)
                {
        //If all is well we can  assign the value of POST field to a variable
                $userCompany = $_POST['userCompany'];
                }
        else
                {
        // if all is not well, we echo an error and exit the script
                echo '<font color="red">Please enter your business\'s name</font>';
                
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright ©  2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

			exit();
                }

	// check the POST variable userFName is sane, and is not empty
        if(empty($_POST['userFName'])==FALSE && sanityCheck($_POST['userFName'], 'string', 25) != FALSE)
                {
        //If all is well we can  assign the value of POST field to a variable
                $userFName = $_POST['userFName'];
                }
        else
                {
        // if all is not well, we echo an error and exit the script
                echo '<font color="red">Please enter your first name</font>';
                
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright © 2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

			exit();
                }

	// check the POST variable userLName is sane, and is not empty
        if(empty($_POST['userLName'])==FALSE && sanityCheck($_POST['userLName'], 'string', 25) != FALSE)
                {
        //If all is well we can  assign the value of POST field to a variable
                $userLName = $_POST['userLName'];
                }
        else
                {
        // if all is not well, we echo an error and exit the script
                echo '<font color="red">Please enter your last name</font>';
                
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright ©  2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

			exit();
                }
        // check the POST variable userAddress1 is sane, and is not empty
        if(sanityCheck($_POST['userAddress1'], 'string', 100) != FALSE)
                {
        //If all is well we can  assign the value of POST field to a variable
                $userAddress1 = $_POST['userAddress1'];
                }
        else
                {
        // if all is not well, we echo an error and exit the script
                echo '<font color="red">Please enter your business\'s address</font>';
                
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright ©  2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

			exit();
                }
	// here we test for the sanity of userAddress2, we dont need to stop the
        // the script if it is empty as it is not a required field.
        if(sanityCheck($_POST['userAddress2'], 'string', 100) != FALSE)
                {
        // if all is well we assign the userAddress to a variable
                $userAddress2 = $_POST['userAddress2'];
                }
        else
                {
        // if all is not well, we simply give the userAddress a blank value
                $userAddress2 = '';
                }
        // here we test for the sanity of userCity.
        if(sanityCheck($_POST['userCity'], 'string', 25) != FALSE)
                {
        //If all is well we can  assign the value of POST field to a variable
                $userCity = $_POST['userCity'];
                }
        else
                {
        // if all is not well, we echo an error and exit the script
                echo '<font color="red">Please enter what city your business resides in</font>';
                
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright ©  2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

			exit();
                }
        // here we test for the sanity of userState.
        if(sanityCheck($_POST['userState'], 'string', 25) != FALSE)
                {
        //If all is well we can  assign the value of POST field to a variable
                $userState = $_POST['userState'];
                }
        else
                {
        // if all is not well, we echo an error and exit the script
                echo '<font color="red">Please enter what state your business resides in</font>';
                
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright ©  2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

			exit();
                }
        // check the sanity of the number and that it is greater than zero and 5 digits long
        if(sanityCheck($_POST['userZip'], 'numeric', 5) != FALSE && checkNumber($_POST['userZip'], 5) == TRUE)
                {
        //If all is well we can  assign the value of POST field to a variable
                $userZip = $_POST['userZip'];
                }
        else
                {
        // if all is not well, we echo an error and exit the script
                echo '<font color="red">Please enter what zip code your business resides in</font>';
                
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright ©  2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

			exit();
                }
        // check the sanity of the number and that it is greater than zero and 12 digits long
      if(sanityCheck($_POST['userPhone'], 'numeric', 10) != FALSE && checkNumber($_POST['userPhone'], 10) == TRUE)
                {
        //If all is well we can  assign the value of POST field to a variable
                $userPhone = $_POST['userPhone'];
                }
        else
                {
        // if all is not well, we echo an error and exit the script
                echo '<font color="red">You either did not supply a phone number for your business 
				  or you did not use the correct format.<br /> 
				  The correct format is 3305551234 excluding the dashes (-)</font>';
                
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright ©  2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

			exit();
                }
        // check the sanity of the number and that it is greater than zero and 12 digits long
      if(sanityCheck($_POST['userFax'], 'numeric', 10) != FALSE && checkNumber($_POST['userFax'], 10) == TRUE)
                {
        // if all is well we assign the userAddress to a variable
                $userFax = $_POST['userFax'];
                }
        else
                {
        // if all is not well, we simply give the userAddress a blank value
                $userFax = '';
                }
    // check the sanity of the userEmail sent from the form
       if(sanityCheck($_POST['userEmail'], 'string', 25) != FALSE && checkEmail($_POST['userEmail']) != FALSE)
                {
        // if the checks are ok for the email we assign the email address to a variable
                $userEmail = $_POST['userEmail'];
                }
        else
                {
        // if all is not well we echo an error message
                echo '<font color="red">Invalid Email Address Supplied. The email address must start 
			with a letter and then can have a combination of underscores(_), periods(.), 
			and dashes(-) @ example.com</font>';
        
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright ©  2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

	// and exit the script
                exit();
                }

       // check the sanity of the userWebsite sent from the form
       if(sanityCheck($_POST['userWebsite'], 'string', 25) != FALSE && checkWebsite($_POST['userWebsite']) != FALSE)
                {
        // if the checks are ok for the email we assign the email address to a variable
                $userWebsite = $_POST['userWebsite'];
                }
        else
                {
        // if all is not well we echo an error message
                echo '<font color="red">Invalid Email Address Supplied. The email address must start 
			with a letter and then can have a combination of underscores(_), periods(.), 
			and dashes(-) @ example.com</font>';
        
			// Add end of the document stuff
			echo '
			</div><!--end of content div -->
     			<div id="footer">
       				Copyright © 2010
     			</div><!--end of footer div -->

			</div> <!--end of container div -->        
    
			</body>
			</html>';

	// and exit the script
                exit();
                }	

        // if all is well we mail off a little thank you email. We know it is
        // safe to do so because we have validated the email address.
            $to = "[email protected]"; // This is the person who is reciving the email
		$subject = "Join Form"; //Subject line of the email

		//This is where the details of the applicant are displayed
		$msg = "This Form contains details of an individual who wishes to become a member of the UBA\n
	         Company Name: '$userCompany'\n
			 First Name:   '$userFName'\n
			 Last Name:    '$userLName'\n
			 Address1:     '$userAddress1'\n
			 Address2:     '$userAddress2'\n
			 City:         '$userCity'\n
			 State:        '$userState'\n
			 Zip Code:     '$userZip'\n
			 Phone:        '$userPhone'\n
			 Fax:          '$userFax'\n
			 Email:         '$userEmail'\n
			 How Did you Hear About US:\n\n '{$_POST['userHearFrom']}'\n";

		$from = "[email protected]"; // email of the person who is taking care of the website
		$headers = "From: $from";


            if(!mail($to,$subject,$msg,$headers))
            {
            echo '<font color="red">Unable to send  Information!</font>';
            }
        else
            {
            echo 'Thank you for your submission, a confirmation email will be sent to '.$userEmail;
            }
        }
    
  else
        {
        // this will be the default message if the form accessed without POSTing
        echo '<p>Please fill in the form above</p>';
        }


?>
     
     </div><!--end of content div -->
     <div id="footer">
       Copyright ©  2010
     </div><!--end of footer div -->

</div> <!--end of container div -->        
    
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/196043-problems-validating-email-address/
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.