Regular expression for e-mail validation

24

I'm trying to create a regular expression to validate any email, I wrote the expression below, but it is not working as expected:

var parse_email = /^[a-z0-9.]+@[a-z0-9]+\.[a-z]+\.([a-z]+)?$/i;

What I expected from each section:

  • [a-z0-9.]+ - part before @ of the email, name of the user;
  • @ - obligatory character at arroba;
  • [a-z0-9]+ - part after @ of the email, name of the provider;
  • \. - dot character after provider name;
  • [a-z]+ - usually where .com is placed;
  • \. - dot character after .com , should only be mandatory if there is for example a .br or abbreviation of any other country at the end of the email;
  • ([a-z]+)? - usually where the abbreviation of the country is placed.

How I tested the expression:

var espacos = '                           ';
var parse_email = /^[a-z0-9.]+@[a-z0-9]+\.[a-z]+\.[a-z]?$/i;
console.log("[email protected]" + espacos.substring("[email protected]".length) + parse_email.test("[email protected]"));
console.log("[email protected]" + espacos.substring("[email protected]".length) + parse_email.test("[email protected]"));
console.log("[email protected]" + espacos.substring("[email protected]".length) + parse_email.test("[email protected]"));
console.log("foo.bar@gmail." + espacos.substring("foo.bar@gmail.".length) + parse_email.test("foo.bar@gmail."));
console.log("foo.bar@gmailcom" + espacos.substring("foo.bar@gmailcom".length) + parse_email.test("foo.bar@gmailcom"));
console.log("foo.bargmail.com" + espacos.substring("foo.bargmail.com".length) + parse_email.test("foo.bargmail.com"));
console.log("@gmail.com" + espacos.substring("@gmail.com".length) + parse_email.test("@gmail.com"));
    
asked by anonymous 28.12.2013 / 20:00

6 answers

26

First you need to accept that you will not be able to process all possible emails. Their specification is long and complicated. For example, here is a regex that accepts all emails and nothing else: link

With this in mind, you start off to make a regex that can hit in most cases .

/^[a-z0-9.]+@[a-z0-9]+\.[a-z]+\.([a-z]+)?$/i

(regexplained)

Your mistake here was just to not include the last \. inside the parentheses. Doing this I have this result:

[email protected]       true
[email protected]    true
[email protected] false
zignd.igor@gmail.          false
zignd.igor@gmailcom        false
zignd.igorgmail.com        false
@gmail.com                 false

That I believe to be what you seek. But this will fail in several other cases. As if the email includes underlines or + , or if the domain includes many characters (like some of the [email protected] government, I've had problems with those).

A more complete suggestion, coming from HTML5 specification by W3C :

  

The following JavaScript- and Perl-compatible regular expression is an implementation of the above definition.   

/^[a-zA-Z0-9.!#$%&'*+/=?^_'{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

(regexplained)

Remember that validations in # should not be considered trustworthy, especially in javascript, as the user can change the code and circumvent the validation. There should always be validation done by the server, usually by sending a confirmation email. Usually client validation is used only to improve the user experience, showing what is wrong without waiting for a response from the server.

    
28.12.2013 / 20:39
11

E-mail Validation Difficulties

Validating emails with RegEx, even more via Javascript, can be a double-edged sword. Valid e-mails can be rejected and invalid can be accepted in most commonly used expressions on websites worldwide.

On the other hand, it is important to understand that it is not necessary, nor is it advisable, to validate an email very rigidly, after all we will only know if it is truly valid when sending an email to the address in question. >

An interesting discussion about email validation can be found in this link .

Validation with Regex

A regular expression that properly validates an e-mail according to the official e-mail definition can be found in this link . However, it is very complex and probably not supported by Javascript engines .

A simplification can be found in the article quoted above (first link), which would be:

/[a-z0-9!#$%&'*+/=?^_'{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_'{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/gi

See the jsfiddle .

Note that the suffix /gi at the end. i causes upper and lower case emails to be accepted. The g causes the expression to check the String as a whole. Without g , an email like [email protected] [email protected] would be accepted.

Setting the Regular Expression of the Question

As for the original expression of the question, a small adjustment would make it work:

/^[a-z0-9.]+@[a-z0-9]+\.[a-z]+(\.[a-z]+)?$/i

Note that the last opened parenthesis is now before the last endpoint character, which should also be optional. On the other hand, this expression will have many false positives. The above expression is more appropriate.

See the jsfiddle .

Conclusion

The regular expression proposed in the question can be easily corrected, but it will invalidate valid emails from being accepted. The other quoted phrase above is more recommended. Although emails like [email protected] are accepted, they are in theory perfectly valid.

    
28.12.2013 / 20:50
4

Two expressions that I use without any problem are:

"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+).(\.[a-z]{2,3})$"

"^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$"

This way:

 <script type="text/javascript">
function validateEmail(email)
{
 var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
 if (reg.test(email)){
 return true; }
 else{
 return false;
 }
} 
</script> 

However, it is no longer interesting to use these validations, since they are very wrong with the new domains and extensions. I advise you to take a look at the Douglas Lovell article , which is for PHP but easily converts for JS.

    
28.12.2013 / 20:35
0

Create a function with the name example: validEmail . This function should receive the email per parameter and verify that it is a valid email.

The user name (before the at sign) can be any alphanumeric character, including underscore, "+" sign and dot

After the at sign, the domain can contain only alphanumeric characters and the underscore;

For extension, the domain must be followed by a period, and at least 2 alphanumeric characters;

The end of the domain is optional, but if it exists, it must start with a dot, followed by a maximum of 2 alphanumeric characters

After the explanation, the javascript code of the function with regex

function validEmail(email){
    return /^[\w+.]+@\w+\.\w{2,}(?:\.\w{2})?$/.test(email)
}

This function will return true or false

    
13.02.2018 / 13:41
0

With HTML5 syntax it is also possible to do a validation in the same HTML by setting the input type to "email".

<input type="email" name="email"/>
    
19.09.2018 / 16:54
-4

You can use it without fear, it even works in Typescript.

[A-Za-z0-9\._-]+@[A-Za-z0-9]+\..(\.[A-Za-z]+)*
    
16.09.2016 / 18:43