Rails regex email validation with no spaces allowed

Viewed 5970

I am currently using this code for my email validation:

/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

the problem is it still accepts emails with spaces like ab c@gmail.com.

I've tried a lot of variations of the code that should not allow spaces, but instead of refreshing the page with an error message at the top of the form, it gives me this error:

The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?

5 Answers

Please try this regex once. I have tried that this regex solves the check of whitespaces, invalid "@" or "." characters (mostly duplicates).

^(?=.{0,64}@(\S))[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)*[^-][A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{1,})(\S)$

I hope it solves the issue.

Hoping for improvement comments.

Related