2023-10-06 11:58:24 +01:00
|
|
|
package validate
|
|
|
|
|
|
|
|
|
|
import (
|
2026-09-07 13:21:17 +01:00
|
|
|
"net/mail"
|
2023-10-06 11:58:24 +01:00
|
|
|
)
|
|
|
|
|
|
2023-10-07 13:42:17 +01:00
|
|
|
var (
|
2023-11-19 20:42:48 +00:00
|
|
|
ErrInvalidEmail = NewError("invalid email address")
|
2023-10-07 13:42:17 +01:00
|
|
|
)
|
|
|
|
|
|
2026-09-07 13:21:17 +01:00
|
|
|
// Email validates an email address using net/mail.ParseAddress.
|
|
|
|
|
//
|
|
|
|
|
// Only a bare address is accepted (alice@example.com). Anything with a
|
|
|
|
|
// display name, angle brackets, a comment, or trailing content is rejected,
|
|
|
|
|
// as is a list of addresses. Note that ParseAddress does not require the
|
|
|
|
|
// domain to have a dot, so "alice@localhost" is considered valid; layer on
|
|
|
|
|
// Match or a DNS check if you need to be stricter.
|
2023-10-06 11:58:24 +01:00
|
|
|
func Email(value string) error {
|
2026-09-07 13:21:17 +01:00
|
|
|
addr, err := mail.ParseAddress(value)
|
|
|
|
|
if err != nil || addr.Name != "" || addr.Address != value {
|
2023-10-07 13:42:17 +01:00
|
|
|
return ErrInvalidEmail
|
2023-10-06 11:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|