diff options
Diffstat (limited to 'validator.go')
-rw-r--r-- | validator.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/validator.go b/validator.go new file mode 100644 index 0000000..d6a4bb5 --- /dev/null +++ b/validator.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "regexp" +) + +var specialsRxp *regexp.Regexp = regexp.MustCompile("[,.\\[\\]!@#$%^&*?_\\(\\)-]") +var numbersRxp *regexp.Regexp = regexp.MustCompile("[0-9]") + +func validatePassword(password string) error { + if len(password) < 10 { + return fmt.Errorf("Your password needs to be at least 10 characters long. But was %d characters.", len(password)) + } + // check uppercase and lowercase + if match, err := regexp.MatchString("[a-z]", password); err != nil || !match { + return fmt.Errorf("Your password needs at least one lowercase character") + } + if match, err := regexp.MatchString("[A-Z]", password); err != nil || !match { + return fmt.Errorf("Your password needs at least one uppercase character") + } + // check specials + numbers (at least 3) + specials := specialsRxp.FindAllString(password, -1) + numbers := numbersRxp.FindAllString(password, -1) + if len(specials)+len(numbers) < 3 { + return fmt.Errorf("Your password needs at least three special characters or numbers") + } + + // call out to password hash check service... + return nil +} |