Email validation using ruby

The following piece of code validates any email that ends with .com, .edu, .org or any three letter extension. It uses regular expressions to find a match.


These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blinkbits
  • BlinkList

6 Comments »

  1. Cheba said,

    May 9, 2007 @ 6:35 am

    ‘ test@ test .com’ =~ /(.+)@(.+)\.(.{3})/
    Note the spaces.

    /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i
    This one is more exact but is still not one that follows RFC.

  2. John said,

    September 13, 2007 @ 9:58 pm

    This should allow for two letter domains, to take domains like .tv and .sc into account.

  3. Validação de email com Ruby : Ruby Brasil said,

    November 22, 2007 @ 4:08 pm

    [...] via rubyonrailsexamples.com [...]

  4. Harry said,

    April 2, 2008 @ 1:38 am

    %r{^(?:[_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-zA-Z0-9\-\.]+)*(\.[a-z]{2,4})$}i

    this is the most appropriate one… with all the validations including the domain extensions…

  5. Randal L Schwartz said,

    April 18, 2008 @ 5:35 pm

    As usual, every SINGLE email regex matcher here has FAILED to read RFC822. As with all the other versions, THIS REGEX IS WRONG. DO NOT USE THIS REGEX. DO NOT. AND PLEASE STOP POSTING BROKEN REGEX TO MATCH EMAIL HERE. {sigh}

  6. Ezra Osiris said,

    July 4, 2008 @ 8:14 am

    Allows two letters domains please use of email =~ /(.+)@(.+)\.(.{2})/ instead of
    email =~ /(.+)@(.+)\.(.{3})/ for three letters domains.

    Thanks !

RSS feed for comments on this post · TrackBack URI

Leave a Comment