I'm currently trying to validates an email attributes with few things :
I'm stuck at the fourth step, I don't really know how to implement it, the main of this steps is to exclude throwable mail providers.
I've currently this :
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false },
:exclude => Not working when I put a regex here
My problem isn't the regex, it's how to exclude email matching with exclude regex.
Can you help me do this ?
Cordially, Rob.
If you use devise for user authentication you can uncomment code in devise.rb
# Email regex used to validate email formats. It simply asserts that
# one (and only one) @ exists in the given string. This is mainly
# to give user feedback and not to assert the e-mail validity.
# config.email_regexp = /\A[^@]+@[^@]+\z/
otherwise i think you can write like
in model
validates :email, uniqueness: true
validate :email_regex
def email_regex
if email.present? and not email.match(/\A[^@]+@[^@]+\z/)
errors.add :email, "This is not a valid email format"
end
end
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