Skip to content

Instantly share code, notes, and snippets.

@ngelx
Last active June 7, 2023 08:54
Show Gist options
  • Save ngelx/3bcce7c1ebe16d58149e79b94c6159f3 to your computer and use it in GitHub Desktop.
Save ngelx/3bcce7c1ebe16d58149e79b94c6159f3 to your computer and use it in GitHub Desktop.
Regexp to validate emails in Ruby
# Strictly adhere to email address standards and disallow hyphens at the beginning or end of the domain name.
# Use https://rubular.com for live testing.
def validate_email(string)
(string.downcase =~ /\A[a-zA-Z0-9._%+-]+@[a-zA-Z0-9](?:[a-zA-Z0-9\-_]*[a-zA-Z0-9])?(?:\.[a-zA-Z0-9]*)(?:\.[a-zA-Z]{2,})+\z/)&.zero?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment