-
Notifications
You must be signed in to change notification settings - Fork 4
/
regex.go
27 lines (22 loc) · 803 Bytes
/
regex.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
26
27
package govalidator
import "regexp"
const (
// Regex represents rule name which will be used to find the default error message.
Regex = "regex"
// RegexMsg is the default error message format for fields with Regex validation rule.
RegexMsg = "%s is not valid"
)
// RegexMatches checks if the given value of s under validation matches the given regular expression pattern.
//
// Example:
//
// v := validator.New()
// v.RegexMatches("example123", "[a-z]+[0-9]+", "input", "input must contain letters followed by numbers.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) RegexMatches(s string, pattern string, field, msg string) Validator {
r := regexp.MustCompile(pattern)
v.check(r.Match([]byte(s)), field, v.msg(Regex, msg))
return v
}