E-mail validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Midgard
    New Member
    • Nov 2006
    • 10

    E-mail validation

    Hi there,

    I'm using this code for e-mail checking:

    [PHP]
    if (!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email)) {
    echo "<span style=\"color: red; font-weight: bold; \">Registrat ion errors have occured:</span><br />You entered an invalid email address.<p>Plea se go <a href=\"javascri pt:history.go(-1)\">previous page</a> and try again.</p>";
    } else {
    // registration codes
    }
    [/PHP]

    But, example, if my email address is hello.there@add ress.net registration have error. Why?

    Thanks
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Your regular expression only allows alphabetic chars and numeric digits before the @.
    In ordet to handle that use the pattern in the following function:
    Code:
    function valid_email ($str) {
     return  (ereg ('(^[0-9a-zA-Z_\.-]{1,}@([0-9a-zA-Z_\-]{1,}\.)+[0-9a-zA-Z_\-]{2,}$)', $str));
    }
    Ronald :cool:

    Comment

    Working...