golang对输入表单简单验证

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

使用很方便:

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())
}

检测项目

输入字符检测

示例

userInput := validator("blah!blah!blah!")
userInput.Required().oneLowerCase().check() // false

多重检测

userInput.minLength(8).oneLowerCase().oneUpperCase().oneNumber().isSpecialCharacter().check()

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

Relative Articles
本文网址: https://www.golangnote.com/topic/249.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。