Badr Tazi
Badr Tazi

Reputation: 759

How to confirm (not verify) an email address with devise in Rails 4?

I'm using devise and I'm trying to add an email_confirmation input, I want my users to type their email address twice in order to make sure that they didn't make any mistake (like the password_confirmation input). I have searched for a solution for days but all I can find is how to "verify" an email address. How would the validation work ? Any answer/suggestion will be greatly appreciated.

Upvotes: 0

Views: 1587

Answers (3)

wesley6j
wesley6j

Reputation: 1403

To confirm their email address, add the field email_confirmation to the form:

run rails generate devise:views so that devise views are available within the application.

add email_confirmation to the devise form.

Then allow this parameter to be passed to devise: https://github.com/plataformatec/devise#strong-parameters

The last step is to add the validation to User model (or the model you use with devise): validates :email, confirmation: true

Upvotes: 1

DJS
DJS

Reputation: 13

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }

Upvotes: 0

joshua.paling
joshua.paling

Reputation: 13952

You'll want to use a regular expression validation. Something like this should do the trick:

validates :email,
format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, message: 'Invalid email' }

That's assuming that by "verify" you mean simply "check that the thing the user entered looks like a valid email".

There's an additional step of verification, not taken by most apps, which checks that the email not only looks like a valid email, but actually is an email address that exists in real life. If that's what you're after, check out https://github.com/kamilc/email_verifier

Upvotes: 0

Related Questions