Tuesday, September 20, 2022

[ Email validation using Regular Expression ]


The module Regular Expression in Python provides a way to match patterns and I am using it to create my own validator for inputted email addresses. I have spent some time over the pattern that checks an email address and I created a string that is very useful in detecting whether a given email is valid or invalid. It is far from perfect, as I have seen the same string to occupies few lines of code when developed by companies like Google for example, but mine is also effective in catching a lot of fake email addresses or even strings that are not an email address at all. It can be used in web forms where Python code can be implemented in background.

Here is my Regular Expression pattern to validate an email address:

r"^[a-z0-9]+\.?[a-z0-9_]+\.?[a-z0-9]+@[a-z0-9\-]+\.?[a-z0-9\-]+\.[a-z]+$"

It can successfully detect most of the valid email addresses, including those using a dot . in the email name part. I have not included all the allowed by the convention special symbols as by my opinion they are rare in use by common users but same symbols could be easily added to the expression.

My code can also successfully detect many cases of invalid email addresses or attempts by the user to input something only to bypass the usually required field of the email address in various web or other forms.

I will not stop adding further improvements to this code so it will become better in the future. If you have questions or just want to say something about this code, you can use the contact form on the right. I am open to any kind of collaboration as well.