Skip to content

Instantly share code, notes, and snippets.

@MorenoMdz
Created September 20, 2018 02:35
Show Gist options
  • Select an option

  • Save MorenoMdz/fb3aff1e8a740443a1bea80c02b4cc9b to your computer and use it in GitHub Desktop.

Select an option

Save MorenoMdz/fb3aff1e8a740443a1bea80c02b4cc9b to your computer and use it in GitHub Desktop.
Email validation util.
// 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;
};
@MorenoMdz
Copy link
Copy Markdown
Author

Usage example with React-Form:
errors.emails = validateEmails(values.emails || '');

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