diff --git a/README.md b/README.md index db026b6c2..befac0394 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ * [Unique values of array](#unique-values-of-array) * [URL parameters](#url-parameters) * [UUID generator](#uuid-generator) +* [Validate email](#validate-email) * [Validate number](#validate-number) * [Value or default](#value-or-default) @@ -768,6 +769,17 @@ const uuid = _ => // uuid() -> '7982fcfe-5721-4632-bede-6000885be57d' ``` +### Validate email + +Use a regular experssion to check if the email is valid. +Returns `true` if email is valid, `false` if not. + +```js +const validateEmail = str => + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str); +// validateEmail(mymail@gmail.com) -> true +``` + ### Validate number Use `!isNaN` in combination with `parseFloat()` to check if the argument is a number. diff --git a/snippets/validate-email.md b/snippets/validate-email.md index b4037d4aa..d83a41904 100644 --- a/snippets/validate-email.md +++ b/snippets/validate-email.md @@ -1,11 +1,10 @@ ### Validate email - + Use a regular experssion to check if the email is valid. Returns `true` if email is valid, `false` if not. - - ```js -const validateEmail = str => + +```js +const validateEmail = str => /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str); - // isemail(mymail@gmail.com) -> true - ``` - +// validateEmail(mymail@gmail.com) -> true +```