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?
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.
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.
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.
I use the constant built into URI in the standard ruby library
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With