验证邮箱地址是什么意思_邮箱绑定手机号步骤

验证邮箱地址是什么意思_邮箱绑定手机号步骤,第1张

验证邮箱地址是什么意思_邮箱绑定手机号步骤 在用户提交邮箱地址以后我们需要验证用户邮箱地址是否合法,解决方案的范围很广,可以通过使用正则表达式来检查电子邮件地址的格式是否正确,甚至可以通过尝试与远程服务器进行交互来解决问题。

两者之间也有一些中间立场,例如检查顶级域是否具有有效的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 := "[email protected]" 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)}稍微更好的解决方案: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 := "[email protected]"; !isEmailValid(e) { fmt.Println(e + " is not a valid email") } // Real domain if e := "[email protected]"; !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}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/650463.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-17
下一篇 2022-04-17

发表评论

登录后才能评论

评论列表(0条)

保存