Skip to main content
Ask A Question
You’re not receiving notifications from this thread.

validate presence of specific domain in email

General • Asked by John Munyi

Hi there, is a way via regex to validate an email and make sure it belongs to a specific domain, I found the below regex on SO and it doesnt seem to do the job.

validates :email, format: { with: /\b[A-Z0-9._%a-z\-][email protected]\.org\z/, message: "must be a account" }

Your code is looking for but your error message says Is that causing the problem?

Other than that, if you don't want to use regex you could split the email string on the @ symbol and verify the last string in that array matches

I used "mydomain" just as an example ... i had replaced all that with the specific domain.

will try with @ and the domain separated

I get that but I just wanted to point out in your Regex you're looking for .org and your error message says your looking for .com. So, if you're passing in .com email addresses it won't save that object because it doesn't pass your :email validation, which could be why it's not working.

The posted regex should work so long as you are passing in the whitelisted FQDN into the regex pattern.

When in doubt, practice your regex patterns on Rubular

Finally worked , the issue was separating "@" and the "" part i used
[email protected]

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.