EMail Validations--Multiple Regexes

6 views
Skip to first unread message

Kaushik Tiwari

unread,
Aug 25, 2013, 1:32:16 PM8/25/13
to rubyonra...@googlegroups.com

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

Colin Law

unread,
Aug 27, 2013, 4:23:27 AM8/27/13
to rubyonra...@googlegroups.com
On 25 August 2013 18:32, Kaushik Tiwari <kaushik...@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
Reply all
Reply to author
Forward
0 new messages