DEV Community

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

Posted on

3 3

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},
    format:{with:VALID_EMAIL_REGEX,multiline:true}
Enter fullscreen mode Exit fullscreen mode

view it on rubular
https://rubular.com/r/DofEroQvHSXNIw

Top comments (2)

Collapse
 
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.

davidcel.is/posts/stop-validating-...

Collapse
 
bizzibody profile image
Ian bradbury • Edited

Fails to validate:
xxx.xxx+@xxx.xxx

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more