The Ruby Email Validation Regex

Validating a user’s email is a pretty common tasks when building any web application. However, I’m pretty bad with regular expressions, so I usually turn to my good friend, the Ruby on Rails tutorial, to use the one that Michael Hartl provides. Since I’ve done this a couple of times now (go through the tutorial trying to find the email regex), I want to post it here for my and your convenience:

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }

Enjoy!

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.