Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email validation in Ruby on Rails?

I am doing email validation in Rails with:

validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i 

Also, I do HTML5 validation in the frontend but email addresses like

[email protected] [email protected] 

still are valid. What am I missing?

like image 384
Haseeb Ahmad Avatar asked Jul 27 '16 11:07

Haseeb Ahmad


People also ask

How to check if an email is valid in Ruby?

Having an @ symbol is a must for an email ID to be valid. In addition to this, there are prescribed formats for prefixes and domains in an email ID. Prefix format: Letters, numbers, underscores, periods, and dashes can be characters of prefixes.

How does validation work in Rails?

Before saving an Active Record object, Rails runs your validations. If these validations produce any errors, Rails does not save the object. After Active Record has performed validations, any errors found can be accessed through the errors instance method, which returns a collection of errors.

How do I validate email flutter?

Validating the Email Address with email_validator :The static method validate() in the EmailValidator class will be used to validate the email address. It returns a bool value; if the email address is valid, the returned value is true; otherwise, the returned value is false.


1 Answers

I use the constant built into URI in the standard ruby library

validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }  
like image 147
Joshua Hunter Avatar answered Sep 18 '22 13:09

Joshua Hunter