Forum: Ruby on Rails EMail Validations--Multiple Regexes

5bc165652f175a639c3ee77e45df8010?d=identicon&s=25 Kaushik Tiwari (Guest)
on 2013-08-27 10:19
(Received via mailing list)
I am trying to use validates_format_of in my model file to validate an
email address. However, I need to enter two regexes for two special
cases.
Basically, only a few top-level domain names are allowed.

However, it seems according to the source code for validates it only
allows
one :format key and one set of options as the hash value. Is there a way
to
use multiple regexes. I have tried the logical operators but it seems to
accept only the first one. Also, using two validates method on the same
field leads to nothing getting accepted as one violates the other
condition.

So, I want to accept only certain top-level domain names, say google and
yahoo. How do I set up a validates method that does just that and
rejects
all other emails.

Thanks
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-27 10:25
(Received via mailing list)
On 25 August 2013 18:32, Kaushik Tiwari <kaushik.tewari@gmail.com>
wrote:
> I am trying to use validates_format_of in my model file to validate an email
> address. However, I need to enter two regexes for two special cases.
> Basically, only a few top-level domain names are allowed.
>
> However, it seems according to the source code for validates it only allows
> one :format key and one set of options as the hash value. Is there a way to
> use multiple regexes.

Just extend the regex to include both cases.  For example abc|def|xyz
matches abc, def or xyz.

Colin
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.