2

I'd like ask if there's a way to validate a string as an url or email at the same time?

for example:

validates_format_of :string, :with => [url_regex, email_regex]

string = 'http://domain.com' => valid
string = '[email protected]' => valid

thanks in advance!

1
  • You need your own validate method, i don't think rails validation provide OR option here Commented Feb 16, 2012 at 7:07

1 Answer 1

2

According to the Rails API you cannot pass two regexes in an array or something. The solution would be to create a regex that matches both URL's and email addresses or - as Naveed suggests - create a custom validation. That being said, I don't think you can validate a URL with a regex.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks but what i wanted was to check if the string is an url or email at the same time
In that case you would have to create a regex that matches both URL's and email addresses. You cannot pass two regexes.
thanks mischa and naveed. i guess i have to create my own validation after all. i just think that validates_format_of is so much cleaner.
@cece: And use URI from the standard library to help you validate the URL.
@muistooshort thanks. yes that's what i did. URI::regexp([http,https])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.