1

I recently discovered some flaws with my users. Some of the emails registered had some characters with different encodings others than UTF-8. So I'm trying to clean all those emails with gsub. By now I'm trying to capture all records with flaws using this regex. Explanation abou the regex: http://regexr.com/3bati

/\A[^@\s]+@([^@\s]+\.)+[^@\W]+\z/

But I'm not able to capture the following string which I inserted in the database as a flag

"\[email protected]".encode('utf-8')

How can I improve this regex to improve my validation and do not let encodings ruin my login?

3
  • stackoverflow.com/a/703068/2035262 Commented Jul 3, 2015 at 14:09
  • BTW, it’s absolutely unclear what you want to do with these emails, why you voluntary decided not to permit them and why, for God’s sake, you think you get an encoding, other than UTF-8? Commented Jul 3, 2015 at 14:12
  • I'm getting unicode characters due copy-paste. Which goes to the database and is automatically converted to UTF-8. And then my users can't login because " [email protected]" != "[email protected]". I'm not asking for the best regex to validate email but just one to help me out catch those flaws. What I'm wanting to do with these emails is not in the scope of the question. Commented Jul 3, 2015 at 14:24

1 Answer 1

1

As I understood your task, you want to make sure, that the email was entered by the user is what she wanted to enter. I would go with:

"\[email protected]".gsub(/[^\p{ASCII}]/, '').encode('ISO-8859-1')

First of all, you don’t need to assure it’s a valid email. The task differs. Secondary, all non-ascii should be filtered out. That’s likely it.

Of course, you might apply any further email validation check.

NB: #.encode in the end is done to assure there is a valid ISO-8859-1 string left after a sanitarization.

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

Comments

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.