mirror of
https://github.com/discourse/discourse.git
synced 2024-12-05 09:56:30 +08:00
3bf3b9a4a5
We validate the *format* of email addresses in many places with a match against a regex, often with very slightly different syntax. Adding a separate EmailAddressValidator simplifies the code in a few spots and feels cleaner. Deprecated the old location in case someone is using it in a plugin. No functionality change is in this commit. Note: the regex used at the moment does not support using address literals, e.g.: * localpart@[192.168.0.1] * localpart@[2001:db8::1]
12 lines
339 B
Ruby
12 lines
339 B
Ruby
# frozen_string_literal: true
|
|
|
|
class EmailAddressValidator
|
|
def self.valid_value?(email)
|
|
email.match? email_regex
|
|
end
|
|
|
|
def self.email_regex
|
|
/\A[a-zA-Z0-9!#\$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#\$%&'\*+\/=?\^_`{|}~\-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$\z/
|
|
end
|
|
end
|