package form import ( "errors" "net/http" "regexp" ) type HomepageForm struct { Username string Password string Confirm string Key string Error string } func (f *HomepageForm) Validate() error { if f.Password != f.Confirm { return errors.New("password doesn't match confirmation") } if len(f.Username) < 3 { return errors.New("username needs to be at least 3 characters") } match, _ := regexp.MatchString("^[a-z0-9-_]+$", f.Username) if !match { return errors.New("only lowercase letters and digits are accepted for username") } if len(f.Password) < 6 { return errors.New("password needs to be at least 6 characters") } return nil } func NewHomepageForm(r *http.Request) *HomepageForm { return &HomepageForm{ Username: r.FormValue("name"), Password: r.FormValue("password"), Confirm: r.FormValue("password-confirm"), Key: r.FormValue("key"), } }