Validate Email Address?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    #1

    Validate Email Address?

    How can i do a check to make sure an email address entered is valid?
  • hoopy
    New Member
    • Feb 2009
    • 88

    #2
    Regex is what you need. Mine is poor but this is one I have used in the past:

    Code:
    if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
      echo ("Valid");
    else 
      echo ("Bad Email.");
    Some regex expert may not agree though! Its a start anyway.

    Cheers.

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      ok, em.. what does it do?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by hoopy
        Some regex expert may not agree though! Its a start anyway.
        just out of interest, why are you capturing the character classes (doesn't make sense to me)?

        I'm using this one (just another RegEx):
        Code:
        /^[\w.%+\-]+@[[:alnum:].\-]+\.[a-z]{2,4}$/i
        Originally posted by ziycon
        ok, em.. what does it do?
        Code:
        /^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/
        /^ from the beginning
        $/ to the end
        [a-zA-Z0-9] look for any of those characters
        + at least once occurring
        * zero or more occurring
        () remember the captured characters

        Comment

        Working...