邮箱正则表达式验证_go语言验证电子邮件地址是否合法

在用户提交邮箱地址以后我们需要验证用户邮箱地址是否合法,解决方案的范围很广,可以通过使用正则表达式来检查电子邮件地址的格式是否正确,甚至可以通过尝试与远程服务器进行交互来解决问题。两者之间也有一些中间立场,例如检查顶级域是否具有有效的MX记录以及检测临时电子邮件地址。

一种确定的方法是向该地址发送电子邮件,并让用户单击链接进行确认。但是在发送文章之前我们需要对用户的邮箱进行预定义检测。

参考:go语言中文文档:www.topgoer.com

简单版本:正则表达式

基于W3C的正则表达式,此代码检查电子邮件地址的结构。

package mainimport (    "fmt"    "regexp")var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")func main() {    // Valid example    e := "test@golangcode.com"    if isEmailValid(e) {        fmt.Println(e + " is a valid email")    }    // Invalid example    if !isEmailValid("just text") {        fmt.Println("not a valid email")    }}// isEmailValid checks if the email provided passes the required structure and length.func isEmailValid(e string) bool {    if len(e) < 3 && len(e) > 254 {        return false    }    return emailRegex.MatchString(e)}
a512c89cc8dd754478ef1648a4860832.png

稍微更好的解决方案:Regex + MX查找

在此示例中,我们结合了对电子邮件地址进行正则表达式检查的快速速度和更可靠的MX记录查找。这意味着,如果电子邮件的域部分不存在,或者该域不接受电子邮件,它将被标记为无效。

作为net软件包的一部分,我们可以使用LookupMX为我们做额外的查找。

package mainimport (    "fmt"    "net"    "regexp"    "strings")var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")func main() {    // Made-up domain    if e := "test@golangcode-example.com"; !isEmailValid(e) {        fmt.Println(e + " is not a valid email")    }    // Real domain    if e := "test@google.com"; !isEmailValid(e) {        fmt.Println(e + " not a valid email")    }}// isEmailValid checks if the email provided passes the required structure// and length test. It also checks the domain has a valid MX record.func isEmailValid(e string) bool {    if len(e) < 3 && len(e) > 254 {        return false    }    if !emailRegex.MatchString(e) {        return false    }    parts := strings.Split(e, "@")    mx, err := net.LookupMX(parts[1])    if err != nil || len(mx) == 0 {        return false    }    return true}
5181e797ec14cd84b36fc115e59347e4.png
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值