Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Last active March 10, 2021 02:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save juanpablocs/4b9e98775ccd3247d25795d6ef2b1db1 to your computer and use it in GitHub Desktop.
Save juanpablocs/4b9e98775ccd3247d25795d6ef2b1db1 to your computer and use it in GitHub Desktop.
SImple Regex for valid email with Javascript

regex valid js update 2021

const validEmail(str) => !/(\.{2}|-{2}|_{2})/.test(str) && /^[a-z0-9][a-z0-9-_\.]+@([a-z]|[a-z0-9]?[a-z0-9-]+[a-z0-9])\.[a-z0-9]{2,10}(?:\.[a-z]{2,10})?$/.test(str);

validEmail('admin@gmail.com'); //true
validEmail('admin@my-large-domain.news'); //true
validEmail('admin@comercio.com.pe'); //true
validEmail('elonmusk@x.com'); // true (short domain)
validEmail('ud@se.cz'); // true (short domain)
validEmail('admin@mycompany.technology'); // true (large extension)
validEmail('.admin@gmail.com'); //false (not initial dot)
validEmail('admin--admin@gmail.com'); //false (not dashes followed)
validEmail('admin@-myapp-.com'); //false (not dashes initial and end)

more use cases: https://regex101.com/r/2ZKtyk/12/tests

@victorcunya
Copy link

Muy buena tu validación para correos electrónicos, no tienes una para Python porfa 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment