开发者

Rails 3 - Help with a Email Validation Regex

开发者 https://www.devze.com 2023-02-05 21:11 出处:网络
the following REGEX开发者_StackOverflow社区 if provided an email domain that does not match INVALID_EMAILs seems to run indefinitely and lock up the rails server (my local env).

the following REGEX开发者_StackOverflow社区 if provided an email domain that does not match INVALID_EMAILs seems to run indefinitely and lock up the rails server (my local env).

INVALID_EMAILS = %w(gmail.com googlemail.com yahoo.com ymail.com rocketmail.com hotmail.com facebook.com)


  def valid_email_domain(emailAddy)
    reg = Regexp.new /#{User::INVALID_EMAILS.map{|a| Regexp.quote(a)}.join("|")}/
    if emailAddy.scan(reg).size == 0
      return true
    else
      return false
    end
  end

Any regex experts out there that can provide feedback? Thanks

UPDATED:

controller:

  def create

    @user = User.new

    # User EmailVeracity to validate the email address
    email = EmailVeracity::Address.new(params[:user][:email])

    Rails.logger.info 'Email Check Result'
Rails.logger.info valid_email_domain(params[:user][:email])
    Rails.logger.info 'Email Check Result'

    if email.valid?
      Rails.logger.info 'Sign this email up'
    else
      Rails.logger.info 'Bad email, dont touch that'
    end

    respond_to do |format|
      format.html
      format.js
    end

  end
protected

  def valid_email_domain(emailAddy)
    !(emailAddy =~ /#{User::INVALID_EMAILS.map{|a| Regexp.quote(a)}.join("|")}/)
  end

User Model:

INVALID_EMAILS = %w(gmail.com googlemail.com yahoo.com ymail.com rocketmail.com hotmail.com facebook.com)


You're making this way too complicated. This should be enough:

def valid_email_domain(email_address)
  !(email_address =~ /#{INVALID_EMAILS.map{|a| Regexp.quote(a)}.join("|")}/)
end
0

精彩评论

暂无评论...
验证码 换一张
取 消