Skip to content

Instantly share code, notes, and snippets.

@jinliming2
Last active November 19, 2022 08:38
Show Gist options
  • Save jinliming2/4f4b1a437f15d40166e968efaac6ce63 to your computer and use it in GitHub Desktop.
Save jinliming2/4f4b1a437f15d40166e968efaac6ce63 to your computer and use it in GitHub Desktop.
Email Format Checker

Email Format Checker

Check Email Format in Golang and JavaScript.

使用 Golang 和 JavaScript 检查 Email 格式的合法性。

Code From: GitLab

package main
import (
"regexp"
)
var EmailChecker = regexp.MustCompile("^[-!#$%&'*+\\/0-9=?A-Z^_a-z{|}~](\\.?[-!#$%&'*+\\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-?\\.?[a-zA-Z0-9])*\\.[a-zA-Z](-?[a-zA-Z0-9])+$")
func EmailCheck(email string) bool {
if len(email) == 0 || len(email) > 254 {
return false
}
if !EmailChecker.MatchString(email) {
return false
}
parts := strings.Split(email, "@")
if len(parts[0]) > 64 {
return false
}
domainParts := strings.Split(parts[1], ".")
for _, val := range domainParts {
if len(val) > 63 {
return false
}
}
return true
}
const EmailChecker = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-?\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;
export default email => {
if (!email) {
return false;
}
if (email.length > 254) {
return false;
}
if (!EmailChecker.test(email)) {
return false;
}
const parts = email.split("@");
if (parts[0].length > 64) {
return false;
}
const domainParts = parts[1].split(".");
return domainParts.every(part => part.length < 64);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment