validate

package module
v0.0.0-...-be1693a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2018 License: MIT Imports: 3 Imported by: 2

README

validate

Build Status Go Report Card GoDoc

Package validate provides functions to validate mobile phone number, username and password.

Documentation
License

Documentation

Overview

Package validate provides functions to validate phone number, username and password.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidIDCardNo

func ValidIDCardNo(IDCardNo string) bool

ValidIDCardNo validate the ID card of the People's Republic of China.

Example
package main

import (
	"log"

	"github.com/northbright/validate"
)

func main() {
	log.Printf("---------- ValidIDCardNo() Test Begin -------------")
	nums := []string{
		"31010419810101400X",
		"310104199001013001",
		"310104600101001",
	}

	for _, v := range nums {
		valid := validate.ValidIDCardNo(v)
		log.Printf("%v: %v", v, valid)
	}

	log.Printf("---------- ValidIDCardNo() Test End -------------")
}
Output:

func ValidMobilePhoneNum

func ValidMobilePhoneNum(number string) bool

ValidMobilePhoneNum validates the mobile phone number in China.

Return:

true for valid or false for invalid.
Example
package main

import (
	"log"

	"github.com/northbright/validate"
)

func main() {
	log.Printf("---------- ValidMobilePhoneNum() Test Begin -------------")
	nums := []string{
		"aaabc89232",
		"10000",
		"13800138000",
	}

	for _, v := range nums {
		valid := validate.ValidMobilePhoneNum(v)
		log.Printf("%v: %v", v, valid)
	}

	log.Printf("---------- ValidMobilePhoneNum() Test End -------------")
}
Output:

func ValidPassword

func ValidPassword(password string, options ...PasswordOption) bool

ValidPassword validates the password.

Params:

password: password string to validate.
          Password consists of numbers, letters(lower or upper) and all chars other than numbers, letters and "_".
options: users can specify options by following functions:
         PasswordMinLen(): min len. Default: 8.
         PasswordMaxLen(): max len. Default: 64.
         PasswordOneNum(): at least one number. Default: false.
         PasswordOneUpper(): at least one upper case letter. Default: false.
         PasswordOneLower(): at least one lower case letter. Default: false.
         PasswordOneSpecial(): at least one symbol or punctuation. Default: false.
Example
package main

import (
	"log"

	"github.com/northbright/validate"
)

func main() {
	log.Printf("---------- ValidPassword() Test Begin -------------")

	passwords := []string{
		"aaa123",
		"Password12",
		"Password2@",
		"aaaabbbb",
		"#ABCD1234",
		"@5431efgh",
		"Copy&Paste中文密码123",
		"CopyPaste日本のパスワード123",
	}

	// Default Password Validation Configuration.
	log.Printf("Test 1: Default Password Validation Configuration:")
	log.Printf("len: 8 - 64, one num: false, one upper: false, one lower: false, one special: false")

	for _, v := range passwords {
		valid := validate.ValidPassword(v)
		log.Printf("%v(len: %v): %v", v, len(v), valid)
	}
	log.Printf("Test 1: Done")

	// Customized Password Validation Configuration.
	log.Printf("Test 2. Customized Password Validation Configuration:")
	log.Printf("len: 9 - 64, one num: true, one upper: true, one lower: true, one special: true")

	for _, v := range passwords {
		valid := validate.ValidPassword(v,
			validate.PasswordMinLen(9),
			validate.PasswordOneNum(true),
			validate.PasswordOneUpper(true),
			validate.PasswordOneLower(true),
			validate.PasswordOneSpecial(true),
		)
		log.Printf("%v(len: %v): %v", v, len(v), valid)
	}
	log.Printf("Test 2: Done")
	log.Printf("---------- ValidPassword() Test End -------------")
}
Output:

func ValidUsername

func ValidUsername(username string, options ...UsernameOption) bool

ValidUsername validates the username. Username can contain unicode letters, latin letters and digits.

Params:

username: username to validate.
options: users can specify options by following functions:
         UsernameMinLen(): min length of username. Default: 6.
             The length of username is the number of bytes in the string(UTF-8 encoded).
             e.g. len("世界") = 6, len("world") = 5.
         UsernameMaxLen(): max length of username. Default: 64.
         UsernameNoNum(): no number in username. Default: false.
         UsernameNoHyphen(): no hyphen in username. Default: false.
         UsernameNoUnderscore(): no underscore in username. Default: false.

Return:

true for valid or false for invalid.
Example
package main

import (
	"log"

	"github.com/northbright/validate"
)

func main() {
	log.Printf("---------- ValidUsername() Test Begin -------------")

	usernames := []string{
		"aaaa",
		"世界",
		"13800138000",
		"a__zzzz",
		"mio--cat",
		"Beyond喜欢你",
		"色褪せぬ蒼青の欠片",
		"#small!!!!!",
		"Michael.Learns.To.Rock",
		" Space Space ",
		"admin@mydomain.com",
	}

	// Default Username Validation Configuration.
	log.Printf("Test 1: Default Username Validation Configuration:")
	log.Printf("username len: 6 - 64, no dot: false, no hyphen: false, no underscore: false.")

	for _, v := range usernames {
		log.Printf("%v(len: %v): %v", v, len(v), validate.ValidUsername(v))
	}
	log.Printf("Test 1: Done")

	// Customized Username Validation Configuration.
	log.Printf("Test 2: Customized Username Validation Configuration:")
	log.Printf("username len: 8 - 64, no dot: true, no hyphen: true, no underscore: true.")

	for _, v := range usernames {
		log.Printf("%v(len: %v): %v",
			v,
			len(v),
			validate.ValidUsername(v,
				validate.UsernameMinLen(8),
				validate.UsernameNoDot(true),
				validate.UsernameNoHyphen(true),
				validate.UsernameNoUnderscore(true),
			),
		)
	}
	log.Printf("Test 2: Done")
	log.Printf("---------- ValidUsername() Test End -------------")
}
Output:

Types

type PasswordOption

type PasswordOption struct {
	// contains filtered or unexported fields
}

PasswordOption specifies an option for password validation.

func PasswordMaxLen

func PasswordMaxLen(l int) PasswordOption

PasswordMaxLen specifies the max length of password.

func PasswordMinLen

func PasswordMinLen(l int) PasswordOption

PasswordMinLen specifies the min length of password.

func PasswordOneLower

func PasswordOneLower(flag bool) PasswordOption

PasswordOneLower specifies if password must have at least one lower case letter.

func PasswordOneNum

func PasswordOneNum(flag bool) PasswordOption

PasswordOneNum specifies if password must have at least one number.

func PasswordOneSpecial

func PasswordOneSpecial(flag bool) PasswordOption

PasswordOneSpecial specifies if password must have at least one special letter. One special letter may be one symbol or one punctuation.

func PasswordOneUpper

func PasswordOneUpper(flag bool) PasswordOption

PasswordOneUpper specifies if password must have at least one upper case letter.

type UsernameOption

type UsernameOption struct {
	// contains filtered or unexported fields
}

UsernameOption specifies an option for username validation.

func UsernameMaxLen

func UsernameMaxLen(l int) UsernameOption

UsernameMaxLen specifies the max length of username.

func UsernameMinLen

func UsernameMinLen(l int) UsernameOption

UsernameMinLen specifies the min length of username.

func UsernameNoDot

func UsernameNoDot(flag bool) UsernameOption

UsernameNoDot specifies if username can have dots.

func UsernameNoHyphen

func UsernameNoHyphen(flag bool) UsernameOption

UsernameNoHyphen specifies if username can have hyphens.

func UsernameNoUnderscore

func UsernameNoUnderscore(flag bool) UsernameOption

UsernameNoUnderscore specifies if username can have underscores.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL