I’m trying to create a Rails 3 validation that will ensure that people are not using one of the common free email addresses.
My thought was something like this ….
validates_format_of :email, :with => /^((?!gmail).*)$|^((?!yahoo).*)$|^((?!hotmail).*)$/
or
validates_exclusion_of :email, :in => %w( gmail. GMAIL. hotmail. HOTMAIL. live. LIVE. aol. AOL. ), :message => "You must use your corporate email address."
But neither works properly. Any ideas?
Basically you’ve written a regex that matches anything. Let’s break it down.
When you think about it you’ll realize that the only strings that won’t match are ones that start with “gmail,” “yahoo,” and “hotmail”–all at the same time, which is impossible.
What you really want is something like this:
Put it together and you have: