Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

simple PHP email validation

  • 18-05-2009 12:07PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 7,516 ✭✭✭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, Registered Users 2 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