Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

simple PHP email validation

Options
  • 18-05-2009 12:07pm
    #1
    Registered Users Posts: 8,070 ✭✭✭


    i want to take in multiple emails

    [PHP]<form method="POST" action="test.php">

    <textarea rows="10" cols="25" name="mails"></textarea>
    <input type="submit" value="submit" name="submit">
    </form>[/PHP]

    [PHP]
    if (isset($_POST)) {
    $all = $_REQUEST["mails"];

    $emails = explode("\n", $all);

    foreach ($emails as $email) {

    if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email)){


    echo "wrong";

    }

    else
    {

    echo "right";
    }

    }

    }

    }



    [/PHP]

    I tried putting !preg_match into a function returning 0 and 1 and calling the function within the foreach loop.

    Doesnt work, seems to display wrong for any first few emails, and correct for last.?


Comments

  • Registered Users Posts: 7,518 ✭✭✭matrim


    Placebo wrote: »
    i want to take in multiple emails

    [PHP]<form method="POST" action="test.php">

    <textarea rows="10" cols="25" name="mails"></textarea>
    <input type="submit" value="submit" name="submit">
    </form>[/PHP]

    [PHP]
    if (isset($_POST)) {
    $all = $_REQUEST["mails"];

    $emails = explode("\n", $all);

    foreach ($emails as $email) {

    if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email)){


    echo "wrong";

    }

    else
    {

    echo "right";
    }

    }

    }

    }



    [/PHP]

    I tried putting !preg_match into a function returning 0 and 1 and calling the function within the foreach loop.

    Doesnt work, seems to display wrong for any first few emails, and correct for last.?

    Here's a function that I use
    function valid_email($str)
    {
       return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }
    

    It also might be worth running each email through trim() before checking it


  • Registered Users Posts: 8,070 ✭✭✭Placebo


    thanks matrim, there was probably white space during split operation.
    So its working now


    better method
    [PHP] function 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;
    }
    [/PHP]


Advertisement