Email regex not valid
Closed this issue · 1 comments
Vadorequest commented
Hi! Another regex issue :)
Here is my regex, in the model:
validates :email, format: { with: /\A\s*(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})[\s\/,;]*)+\Z/i }
Here is the generated data-validate
attribute on the view.
data-validate="[{"kind":"presence","options":{},"messages":{"blank":"skal udfyldes"}},{"kind":"format","options":{"with":"(?i-mx:\\A\\s*(([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})[\\s\\/,;]*)+\\Z)"},"messages":{"invalid":"er ikke gyldig","blank":"skal udfyldes"}}]"
I'm not a pro in regex but it looks like the \
is doubled due to some conversion. And obviously the regex doesn't works with valid emails.
[1] pry(main)> JSON.dump(:with => /abc/)
=> "{\"with\":\"(?-mix:abc)\"}"
Any work around?
Thanks.
Vadorequest commented
The problem was that the email regex was Ruby specific.
/^\s*(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})[\s\/,;]*)+$/i
See https://stackoverflow.com/questions/23383977/email-regex-compatible-with-both-ruby-and-javascript