www.webdeveloper.com
Results 1 to 13 of 13

Thread: email validation not working

  1. #1
    Join Date
    Jun 2006
    Posts
    32

    email validation not working

    Code:
    	// Validate E-Mail Address
    		if (!ereg ('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/',$_POST['email'])) {
    			$problem = TRUE;
    			$errorstr .= '<li>Please enter a <strong>Valid E-Mail Address</strong>.</li>';
    		}
    Can someone tell me what is wrong with this email validation? It's not working...

  2. #2
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    Can you post the whole code?

  3. #3
    Join Date
    Jun 2006
    Posts
    32
    sorry about that!

    Code:
    <?php // Shipping Information Page
    	
    // Include Header
    	require_once ('header.php');
    	
    // In Case register_globals Is Disabled
    	$firstname = $_POST['firstname'];
    	$lastname = $_POST['lastname'];
    	$email = $_POST['email'];
    	$address = $_POST['address'];
    	$city = $_POST['city'];
    	$province = $_POST['province'];
    	$postalcode = $_POST['postalcode'];
    	$phone = $_POST['phone'];
    	$comments = $_POST['comments'];
    	$comments = stripslashes ($comments); // Adjust for Magic Quotes
    	
    // Handle Form
    	if ( isset($_POST['submit'])) { 
    	
    // No Problems So Far
    	$problem = FALSE;
    		
    	// Validate The First Name
    		if (empty ($_POST['firstname'])) { 
       			$problem = TRUE;
        		$errorstr .= '<li>Please enter your <strong>First Name</strong>.</li>'; } 
    	
        // Validate The Last Name
        	if (empty ($_POST['lastname'])) { 
    			$problem = TRUE;
    			$errorstr .= '<li>Please enter your <strong>Last Name</strong>.</li>'; } 
    		
    	// Validate E-Mail Address
    		if (ereg ('/^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$/',$_POST['email'])) {
    			$problem = TRUE;
    			$errorstr .= '<li>Please enter a <strong>Valid E-Mail Address</strong>.</li>';
    		}
    	
    	// Validate The Address
    		if (empty ($_POST['address'])) { 
       			$problem = TRUE;
        		$errorstr .= '<li>Please enter your <strong>Address</strong>.</li>'; } 
    			
    	// Validate The City
    		if (empty ($_POST['city'])) { 
       			$problem = TRUE;
        		$errorstr .= '<li>Please enter your <strong>City</strong>.</li>'; } 
    			
    	// Validate The Province
    		if (empty ($_POST['province'])) { 
       			$problem = TRUE;
        		$errorstr .= '<li>Please enter your <strong>Province</strong>.</li>'; } 
    			
    	// Validate The Postal Code
    		if (empty ($_POST['postalcode'])) { 
       			$problem = TRUE;
        		$errorstr .= '<li>Please enter your <strong>Postal Code</strong>.</li>'; }
    		
    	// Check For Valid Phone Number
    		if (ereg("/^([0-9]{3})-([0-9]{3})-([0-9]{4})$/",$_POST['phone'])) {
    			$problem = TRUE;
    			$errorstr .= '<li>Please enter a <strong>Valid Telephone Number</strong> with the format xxx-xxx-xxxx.</li>';
    		}
    	
    	// If There Weren't Any Problems... 
    		if (!$problem) {
    			// Redirect User To Order Summary
    				header ('Location: confirm.php');
    				exit();
    				} 
    		}
    	
    // Display Order Form Steps
    	print '
    		<table id="steps" summary="Order Form Steps">
    			<tr>
    				<td class="nostep">1. Order Widgets</td>
    				<td class="step">2. Shipping Information</td>
    				<td class="nostep">3. Confirm Order</td>
    			</tr>
    		</table>
    		<br />';
    	
    // If Field Forgotten...
    	if ($problem) {
    		print '
    			<h2>Error</h2>
    				<ul>' . $errorstr . '</ul>
    				<p>Please review your error(s) and fix the problem(s) below. Thank You</p>';
    	}
    	
    // Display Shipping Form
    	print '
    <form action="'.$_SERVER['PHP_SELF'].'" method="post">
    	<fieldset>
    		<legend>Customer Information</legend>
    		<p><span class="req2">*</span> = Required Fields.</p>
    <table summary="shipping information form">
    <tr>
    	<td class="ti"><label for="firstname" accesskey="f"><strong><span class="underline">F</span>irst Name:</strong> <span class="req">*</span></label></td>
    	<td><input type="text" maxlength="52" size="35" name="firstname" id="firstname" value="'.$_POST['firstname'].'" tabindex="1" /></td>
    </tr>
    <tr>
    	<td class="ti"><label for="lastname" accesskey="l"><strong><span class="underline">L</span>ast Name:</strong> <span class="req">*</span></label></td>
    	<td><input type="text" maxlength="52" size="35" name="lastname" id="lastname" value="'.$_POST['lastname'].'" tabindex="2" /></td>
    </tr>
    <tr>
    	<td class="ti"><label for="email" accesskey="e"><strong><span class="underline">E</span>-Mail Address:</strong> <span class="req">*</span></label></td>
    	<td><input type="text" maxlength="52" size="35" name="email" id="email" value="'.$_POST['email'].'" tabindex="3" /></td>
    </tr>
    <tr>
    	<td class="ti"><label for="address" accesskey="a"><strong><span class="underline">A</span>ddress:</strong> <span class="req">*</span></label></td>
    	<td><input type="text" maxlength="52" size="35" name="address" id="address" value="'.$_POST['address'].'" tabindex="4" /></td>
    </tr>
    <tr>
    	<td class="ti"><label for="city" accesskey="c"><strong><span class="underline">C</span>ity:</strong> <span class="req">*</span></label></td>
    	<td><input type="text" maxlength="52" size="35" name="city" id="city" value="'.$_POST['city'].'" tabindex="5" /></td>
    </tr>
    <tr>
    	<td class="ti"><label for="province" accesskey="p"><strong><span class="underline">P</span>rovince:</strong> <span class="req">*</span></label></td>
    	<td><input type="text" maxlength="52" size="35" name="province" id="province" value="'.$_POST['province'].'" tabindex="6" /></td>
    </tr>
    <tr>
    	<td class="ti"><label for="postalcode" accesskey="o"><strong>P<span class="underline">o</span>stal Code:</strong> <span class="req">*</span></label></td>
    	<td><input type="text" maxlength="52" size="35" name="postalcode" id="postalcode" value="'.$_POST['postalcode'].'" tabindex="7" /></td>
    </tr>
    <tr>
    	<td class="ti"><label for="phone" accesskey="h"><strong>P<span class="underline">h</span>one Number:</strong> <span class="req">*</span></label></td>
    	<td><input type="text" maxlength="52" size="35" name="phone" id="phone" value="'.$_POST['phone'].'" tabindex="8" /></td>
    </tr>
    <tr>
    	<td class="ti"><label for="comments" accesskey="m"><strong>Co<span class="underline">m</span>ments:</strong></label></td>
    	<td><textarea name="comments" id="comments" rows="4" cols="32" tabindex="9">'.$_POST['comments'].'</textarea></td>
    </tr>
    </table>
    </fieldset>
    <fieldset>
    <legend>Shipping Options</legend>
    	<p class="center"><input type="radio" name="shipping" id="shippingg" value="5.00" checked="checked" tabindex="18" />
    	<label for="shippingg">Ground (3 Days): <strong>$5.00</strong></label></p>
    <br />
    	<p class="center"><input type="radio" name="shipping" id="shippinga" value="10.00" tabindex="10" />
    	<label for="shippinga">Air (Next Day): <strong>$10.00</strong></label></p>
    </fieldset><br />
    <p class="center"><input type="submit" name="submit" id="submit" class="submit" value="Submit" tabindex="11" />
    <input type="reset" name="clear" id="clear" class="clear" value="Clear" tabindex="12" /></p><br />
    </form>';
    	
    // Include Footer
    require_once ('footer.php');
    ?>

  4. #4
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    Are you getting a php error or is it just accepting any old cr@p?

  5. #5
    Join Date
    Jun 2006
    Posts
    32
    Quote Originally Posted by Dopple
    Are you getting a php error or is it just accepting any old cr@p?
    accepts anything, even a blank field.

  6. #6
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    I'm not a PHP whizz by any strech of the imagination but one thing I've noticed is that you used double quotes for your phone validation and single for the email. Single quotes take the literal meaning of things like variables so
    <?php echo '$fruit'; ?> would print the string $fruit rather than the variable it holds. Try changing the single quotes to inverted commas and see how it goes.

  7. #7
    Join Date
    Jun 2006
    Posts
    32
    Quote Originally Posted by Dopple
    I'm not a PHP whizz by any strech of the imagination but one thing I've noticed is that you used double quotes for your phone validation and single for the email. Single quotes take the literal meaning of things like variables so
    <?php echo '$fruit'; ?> would print the string $fruit rather than the variable it holds. Try changing the single quotes to inverted commas and see how it goes.
    Didn't notice that, but it made no difference.

  8. #8
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654
    In the 1st post you had if(!ereg.... but on your code it if(ereg...
    Also the on the web I found the basic standard to be
    PHP Code:
    if (!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]"$email)) { 
    Edit: sorry I posted a semi colon there. Meant to be a curly bracket.
    Last edited by Dopple; 06-27-2006 at 06:41 AM. Reason: error typing

  9. #9
    Join Date
    Jun 2006
    Posts
    32
    Quote Originally Posted by Dopple
    In the 1st post you had if(!ereg.... but on your code it if(ereg...
    Also the on the web I found the basic standard to be
    PHP Code:
    if (!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]"$email)) { 
    Edit: sorry I posted a semi colon there. Meant to be a curly bracket.
    oddly enough I've tried it both ways... maybe it has something to do with my surrounding code... i'm going to go through and see if I can find any mistakes

  10. #10
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    That regex is miles off! This is probably a lot closer.
    PHP Code:
    $regex "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$"

  11. #11
    Join Date
    Jun 2006
    Posts
    32
    Quote Originally Posted by bokeh
    That regex is miles off! This is probably a lot closer.
    PHP Code:
    $regex "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$"

    Thank you so much! It now works, I had gotten the previous one from a tutorial site... guess it wasn't much of one :P

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,359
    PHP Code:
    # email address validation function
    # kudos to [url]http://iamcal.com/publish/articles/php/parsing_email/pdf/[/url]
    function is_valid_email_address($email) {
      
    $qtext '[^\\x0d\\x22\\x5c\\x80-\\xff]';
      
    $dtext '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
      
    $atom '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
      
    '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
      
    $quoted_pair '\\x5c\\x00-\\x7f';
      
    $domain_literal "\\x5b($dtext|$quoted_pair)*\\x5d";
      
    $quoted_string "\\x22($qtext|$quoted_pair)*\\x22";
      
    $domain_ref $atom;
      
    $sub_domain "($domain_ref|$domain_literal)";
      
    $word "($atom|$quoted_string)";
      
    $domain "$sub_domain(\\x2e$sub_domain)*";
      
    $local_part "$word(\\x2e$word)*";
      
    $addr_spec "$local_part\\x40$domain";
      return 
    preg_match("!^$addr_spec$!"$email) ? 0;

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  13. #13
    Join Date
    Aug 2004
    Location
    70 klicks above the Do Lung bridge
    Posts
    654

    Going to have to check the explanation for that one!!!
    Thanks for including the url there Nog.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles