Jump to content

whats wrong with my email validation - PREG_MATCH


worldcomingtoanend

Recommended Posts

in my php mail script i tried using the first code below and for some reason it keeps giving me "invalid email entered"  i hv changed it to the second code below and i still get the same error.  surprisingly the code works on my personal pc but on my live server it gives me those errors.  where could i be going wrong?  thank u for your help.

 

function is_valid_email($email) {
  return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}

 

 

if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  echo "<h4>Invalid email address</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
  echo "<h4>No subject</h4>";
  echo "<a href='javascript:history.back(1);'>Back</a>";
}

Only issue with eregi is that it is depreciated as of PHP 5.3.0.

 

with ereg I used this and never had any issues:

<?php
function check_email($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; 
  } // End check_email?>

 

To be honest though, using the built in filter_var() function is a good idea if you're on php 5.3.0 :)

<?php
if(filter_var($email, FILTER_VALIDATE_EMAIL) === TRUE) {
    // valid
} else {
    // not valid
}

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.