GolangNote

Golang笔记

golang对输入表单简单验证

Permalink

发现一个很简单、轻量的库,用来验证用户输入的字符串是否合法:

使用很方便:

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main

import (
	"regexp"
	"strconv"
)

type validate struct {
	i string
	r bool
	e string
}

func validator(u string) validate {
	var n validate
	n.i = u
	n.r = true
	n.e = ""
	return n
}

func (s *validate) Required() *validate {
	if s.r == true {
		if s.i == "" || len(s.i) <= 0 {
			s.r = false
			s.e = "Is Required"
		}
	}
	return s
}

func (s *validate) minLength(length int) *validate {
	if s.r == true {
		if len(s.i) < length {
			s.r = false
			s.e = "Minimum length " + strconv.Itoa(length) + " characters allowed"
		}
	}
	return s
}

func (s *validate) maxLength(length int) *validate {
	if s.r == true {
		if len(s.i) > length {
			s.r = false
			s.e = "Maximum length " + strconv.Itoa(length) + " characters allowed"
		}
	}
	return s
}

func (s *validate) isEmail() *validate {
	if s.r == true {
		re := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
		var match = re.MatchString(s.i)
		if !match {
			s.r = false
			s.e = "invalid email"
		}
	}
	return s
}

func (s *validate) oneLowerCase() *validate {
	if s.r == true {
		re := regexp.MustCompile("[a-z]+")
		var match = re.MatchString(s.i)
		if !match {
			s.r = false
			s.e = "does not contain atleast lowercase letter"
		}
	}
	return s
}

func (s *validate) allLowerCase() *validate {
	if s.r == true {
		re := regexp.MustCompile("^[a-z]+$")
		var match = re.MatchString(s.i)
		if !match {
			s.r = false
			s.e = "does not contain lowercase letter"
		}
	}
	return s
}

func (s *validate) oneUpperCase() *validate {
	if s.r == true {
		re := regexp.MustCompile("[A-Z]+")
		var match = re.MatchString(s.i)
		if !match {
			s.r = false
			s.e = "does not contain atleast one uppercase letter"
		}
	}
	return s
}

func (s *validate) allUpperCase() *validate {
	if s.r == true {
		re := regexp.MustCompile("^[A-Z]+$")
		var match = re.MatchString(s.i)
		if !match {
			s.r = false
			s.e = "all letters are not uppercase"
		}
	}
	return s
}

func (s *validate) oneNumber() *validate {
	if s.r == true {
		re := regexp.MustCompile("[0-9]+")
		var match = re.MatchString(s.i)
		if !match {
			s.r = false
			s.e = "does not contain atleast one numeric character"
		}
	}
	return s
}

func (s *validate) isSpecialCharacter() *validate {
	if s.r == true {
		re := regexp.MustCompile("\\`|\\~|\\!|\\@|\\#|\\$|\\%|\\^|\\&|\\*|\\(|\\)|\\+|\\=|\\[|\\{|\\]|\\}|\\||\\|\\'|\\<|\\,|\\.|\\>|\\?|\\/|\"|\\;|\\:|\\s")
		var match = re.MatchString(s.i)
		if !match {
			s.r = false
			s.e = "does not contain atleast one special character"
		}
	}
	return s
}

func (s *validate) check() bool {
	return s.r
}

func main() {
	newInput := validator("golangV$alidation")
	println(newInput.Required().isSpecialCharacter().check())
}

检测项目

输入字符检测

示例

Go: 验证表单示例
1
2
3
userInput := validator("blah!blah!blah!")
userInput.Required().oneLowerCase().check() // false

多重检测

Go: 多重检测
1
userInput.minLength(8).oneLowerCase().oneUpperCase().oneNumber().isSpecialCharacter().check()

Go-Validation 参考 https://github.com/hasanaliqureshi/Go-Validation

本文网址: https://golangnote.com/topic/249.html 转摘请注明来源

Related articles

Golang实现简单的Socks5代理

Socks5 代理较 `http/https` 代理有较好的性能,下面是借鉴某个著名开源软件的 local 实现的简单代理。...

Golang Web 程序生产环境独立部署示例

一个 web 应用通常是跑在一个前端代理,如 Nginx 后,这样可以方便的在同一个服务器部署多个应用。这里说的独立部署是指让 go web 程序直接暴露在外面,独占 443、80 端口(俗称裸跑)。这样做除了性能有些提高外,更重要的是部署方便。...

Write a Comment to "golang对输入表单简单验证"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms