客户递交电子邮箱详细地址后,大家必须查证客户的邮件详细地址是不是合理合法。解决方法范畴很广。我们可以应用正则表达式来查验电子邮箱详细地址文件格式是不是恰当,乃至试着与虚拟服务器互动来解决困难。这彼此之间有一些正中间部位,例如查验顶尖域是不是有合理的MX记录,检验临时性电子邮箱详细地址。

确定的一种办法是向该详细地址推送电子邮箱,并规定客户点击连接开展确定。可是在推送文章内容以前,大家必须事先界定客户的电子邮箱。

简易版本号:正则表达式。

根据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)}验证邮箱地址是什么意思-邮箱绑定手机号步骤-第1张图片

略微更强的解决方法:Regex MX搜索254 {return false}回到email regex.matchstring (e)}略微好一点的解决方法: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}验证邮箱地址是什么意思-邮箱绑定手机号步骤-第2张图片

评论(0条)

刀客源码 游客评论