Created
September 20, 2018 02:35
-
-
Save MorenoMdz/fb3aff1e8a740443a1bea80c02b4cc9b to your computer and use it in GitHub Desktop.
Email validation util.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // It splits by comma then map and trim each emai, then check each email pass the test, keeping the ones that do not pass the test. | |
| // If any email do not pass the regex test show it to the user, otherwise continue (return) | |
| const re = /^(([^<>()\[\]\\.,;:\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,}))$/; | |
| export default emails => { | |
| const invalidEmails = emails | |
| .split(',') | |
| .map(email => email.trim()) | |
| .filter(email => re.test(email) === false); | |
| if (invalidEmails.length) { | |
| return `These emails are invalid: ${invalidEmails}`; | |
| } | |
| return; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example with React-Form:
errors.emails = validateEmails(values.emails || '');