-
Notifications
You must be signed in to change notification settings - Fork 4
/
email.go
25 lines (22 loc) · 1.2 KB
/
email.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package govalidator
const (
// Email represents rule name which will be used to find the default error message.
Email = "email"
// EmailMsg is the default error message format for fields with Email validation rule.
EmailMsg = "%s is not valid"
// EmailRegex is the default pattern to validate email field by RFC 5322 rule.
EmailRegex = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])"
)
// Email checks the value under validation must match the EmailRegex regular expression.
//
// Example:
//
// v := validator.New()
// v.Email("[email protected]", "email", "email address is not valid.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) Email(s, field, msg string) Validator {
v.RegexMatches(s, EmailRegex, field, v.msg(Email, msg, field))
return v
}