DEV Community

Cover image for Regex for email validation in rails
Sourav das
Sourav das

Posted on

Regex for email validation in rails

A simple regex for email validation

VALID_EMAIL_REGEX= /^(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})$/i

    validates :email , presence: true,uniqueness:{case_sensetive:false},
Enter fullscreen mode Exit fullscreen mode

view it on rubular

Top comments (2)

darkain profile image
Vincent Milum Jr

I'd highly suggest NOT using regular expressions for email address syntax validation. The RFC allows for a lot more characters and variance in the email address than what is presented in this particular regular expression, meaning you'll end up excluding some users. And worse, others unaware of this will invariably end up on this article to copy-paste your regex into their application, which will end up them excluding others without even knowing it.

bizzibody profile image
Ian bradbury • Edited

Fails to validate: