author avatar

syedsibtain

Fri Jul 12 2024

Using Ruby's built-in URI::MailTo::EMAIL_REGEXP for email validation is generally better than using a custom regular expression due to its robustness, reliability, and maintenance by the Ruby core team.


class User < ApplicationRecord
  has_secure_password
  validates :name, presence: true

  # Using a custom regular expression for email validation
  validates :email, presence: true, format: { with: /\A[^@\s]+@[^@\s]+\z/ }, uniqueness: true
  
  # Using Ruby's built-in URI::MailTo::EMAIL_REGEXP for email validation
  validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, uniqueness: true

end


#ruby #regex #rails