Skip to content

Instantly share code, notes, and snippets.

@Ch4s3
Created May 11, 2016 18:01
Show Gist options
  • Save Ch4s3/869f4d1fe82984e8d5ef0733ab0d1798 to your computer and use it in GitHub Desktop.
Save Ch4s3/869f4d1fe82984e8d5ef0733ab0d1798 to your computer and use it in GitHub Desktop.
isValidEmail(e) {
/*
* This regex SHOULD capture 99% of valid e-mails, but it is from StackOverflow
* so I can't garuntee that. It works well in tests, but if it fails, lets switch
* to a simple /@/ or use the MailGun API.
*/
const emailRegex = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
const emailFieldValue = e.target.value
if (emailFieldValue.length == 0) {
this.setState({ validEmail: null })
} else {
const valid = emailRegex.test(emailFieldValue);
if (valid == true) {
this.setState({ validEmail: true })
} else {
this.setState({ validEmail: false })
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment