32
I am trying to create a regular expression to validate any e-mail, 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 passage:
[a-z0-9.]+
- part before the@
email, name of the user;@
- mandatory character of arroba;[a-z0-9]+
- part after the@
email, name of the provider;\.
- dot character after the name of the provider;[a-z]+
- usually where is placed the.com
;\.
- dot character after the.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 country abbreviation is placed.
As 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"));
suggestion: validate only the basics, because if you have something annoying for the end user is to try to register with a valid email and fail (I myself have the habit of calling the marketing company by burning the programmer. It seems a little Shiite, but I think a lot about the laypeople who don’t understand what is happening). Have characters, an arroba, more characters, a point at least after and 2 characters or more? Great already. Today has mastery with accent, takes place that can point in the name, takes place with underline, has some with sign of more. Better not complicate (or use updated and complete RFC).
– Bacco