Dingding Ye
1/31/2008 7:07:00 PM
module ActiveRecord
module Validations
module ClassMethods
# Validates whether the value of the specified attribute is a
valid email address
#
# class User < ActiveRecord::Base
# validates_email_format_of :email, :on => :create
# end
#
# Configuration options:
# * <tt>message</tt> - A custom error message (default is: "
does not appear to be a valid e-mail address")
# * <tt>on</tt> Specifies when this validation is active
(default is :save, other options :create, :update)
# * <tt>if</tt> - Specifies a method, proc or string to call to
determine if the validation should
# occur (e.g. :if => :allow_validation, or :if => Proc.new {
|user| user.signup_step > 2 }). The
# method, proc or string should return or evaluate to a true
or false value.
def validates_email_format_of(*attr_names)
configuration = { :message => ' does not appear to be a valid
e-mail address',
:on => :save,
:with =>
/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i
}
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
validates_each(attr_names, configuration) do |record, attr_name, value|
record.errors.add(attr_name, configuration[:message]) unless
value.to_s =~ configuration[:with]
end
end
end
end
end
On Feb 1, 2008 12:14 AM, Eric H. <REMOVE-CAPSburnFORout@setmdaiiol.com> wrote:
> Anyone have a code snippet that tells whether an email address is
> "valid" but not necessarily whether it is a working address because that
> much I can sort out through my mail server through any bounces.
>
> I have a database of addresses where the person entering the data has a
> bad, bad, bad, BAD habit of not validating them before entering them and
> so I get things like "***DO NOT SHARE DATA***" and "lmnopaol.com" and
> the like. It's not my company or else she'd be going over all the
> entries by hand.
>
> Thanks in advance
>
>