email validation
This commit is contained in:
@ -5,5 +5,5 @@ class Account < ApplicationRecord
|
||||
validates :username, presence: true
|
||||
validates :password, presence: true
|
||||
validates :home_folder, presence: true
|
||||
validates :contact_email, presence: true
|
||||
validates :contact_email, presence: true, email_format: true
|
||||
end
|
||||
|
13
app/validators/email_format_validator.rb
Normal file
13
app/validators/email_format_validator.rb
Normal file
@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
class EmailFormatValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
# EMAIL regex test
|
||||
# (comma seperated) [any word combo] AT [any word combo] DOT [2 or more]
|
||||
# me@no.yes.x == invalid
|
||||
# some.thing+two@sub.domain.name == valid
|
||||
results = value.to_s.split(',').map do |v|
|
||||
(v.strip =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i) || v.strip.blank?
|
||||
end
|
||||
record.errors[attribute] << (options[:message] || "is not formatted properly") if results.include?(false)
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user