Module: BrazilianUtils::EmailUtils
- Defined in:
- lib/brazilian-utils/email-utils.rb
Constant Summary collapse
- EMAIL_PATTERN =
Email validation pattern based on RFC 5322/RFC 1035.
This is stricter than brutils/python's reference regex (
^(?![.])[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$), which lets through addresses with a leading/trailing/doubled dot in the local part, or a domain label starting/ending with a hyphen. Each dot-joined segment is validated on its own:- Local part: one or more non-empty segments (letters, digits,
._%+-) joined by single dots - no leading/trailing/consecutive dot - Domain labels: alphanumeric, may contain internal hyphens, but must not start or end with one (RFC 1035 "preferred name syntax")
- TLD: at least 2 letters, no digits or hyphens
- Local part: one or more non-empty segments (letters, digits,
%r{ \A [a-zA-Z0-9_%+-]+ (?: \. [a-zA-Z0-9_%+-]+ )* # local part @ (?: [a-zA-Z0-9] (?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])? \. )+ # domain labels [a-zA-Z]{2,63} # TLD \z }x.freeze
Class Method Summary collapse
-
.is_valid(email) ⇒ Boolean
(also: valid?)
Checks if a string corresponds to a valid email address.
Class Method Details
.is_valid(email) ⇒ Boolean Also known as: valid?
Note:
The validation rules generally follow RFC 5322 specifications:
- Local part cannot start with a dot
- Local part can contain: letters, numbers, dots, underscores, percent, plus, minus
- Must have @ symbol
- Domain can contain: letters, numbers, dots, hyphens
- Must have at least one dot in domain
- TLD must be at least 2 characters and only letters
Checks if a string corresponds to a valid email address.
This function validates email addresses following the specifications defined by RFC 5322, which is the widely accepted standard for email address formats.
54 55 56 57 58 |
# File 'lib/brazilian-utils/email-utils.rb', line 54 def self.is_valid(email) return false unless email.is_a?(String) EMAIL_PATTERN.match?(email) end |